views:

73

answers:

2

I have a web site hosted on an a Apache web server. Another person uploads static html files to a folder each week via an ftp generated from third party software.

I am looking to insert Goggle Analytics code to each page as it is rendered while preserving the URL's, original HTML and process of uploading. I do not know when the files are uploaded or the page names (there are 100's of files uploaded each time).

I initially thought of setting the html to be parsed with the PHP engine however this is not suitable as the pages do not contain PHP code and I do not modify the HTML pages through find and replace.

Is there a way to use the php engine to process the page injecting code just before the end of the page? Or some other approach.

+2  A: 

If you use a .htaccess file to have php parse html files, you can use the php auto prepend/append to add some php code to a file. Check out this and look for auto prepend or append to learn how to automatically include a file. This can be done in a .htaccess also. To get apache to make php parse html files, you can check this out for some info. I just found it through google. You should be able to use both of these to include a php file to all the html files.

Edit: just a note, there doesn't need to be php in the html files for this to work as well. php will just look for the parts of the files with php (which is none of the file) and skip anything that doesn't have php.

Jonathan Kuhn
+4  A: 

Well this would be a simple way, jsut to give you an idea

in .htaccess in the top level directory of the published html files:

RewriteCond %{REQUEST_URI} ^/path/in/question/if/not/document/root/(.+\.html)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+)$ index.php [L]

in index.php:

   $ga = 'your ga tracking code';

   $file = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI'];

   if(file_exists($file))
   {

      $content = file_get_contents($file);

      if(false !== ($pos = strpos($content, '</body>')))
      {
         $content = substr($content, 0, $pos).$ga.substr($content, $pos);
      }

      print $content;
      exit;
   }
prodigitalson
This is looking like the best approach will test it later - Thanks
John
Tested and it work. Thanks
John