views:

94

answers:

4

Hi.

I want to include my PHP file googleanayltics.php on every page on my webserver that is a .php or .html document

I would like to know:

A) How do I add this right BEFORE the </head> tag
B) How do I add right right AFTER the <body> tag

I'd like to know both methods for flexibility

I think .HTACCESS could accomplish this easily, and if you know how, or if you know of any easier method then please do share.

P.S. I do not want to manually go in and enter a code on every file, that is why I am asking this question (for time saving)

A: 

You can append or prepend files by setting a PHP ini value in .htaccess

php_value auto_prepend_file "full_path_to_the_include_directory/prepend.php" 
php_value auto_append_file "full_path_to_the_file_containing_your_analytics_code" 

(It'd be better to set them manually in php.ini)

http://php.net/manual/en/ini.core.php

philfreo
Thanks, where will it prepend the file to. It needs to go specifically right BEFORE the </head> or right AFTER the <body> tags. Would this work like this?
RB. J
+3  A: 

EDIT: Only now I see what you actually wanted (was hidden due to formatting). Although possible, that would be a very clumsy thing to do, and really not worth the effort. See my other answer for a way to do that.


There are two php.ini settings you might be interested in:

auto_prepend_file and auto_append_file

They can be changed via .htaccess (see the other answer).

NOTES:

  • These will be included before or after the whole PHP script; but not in specific sections of the output.

  • They will only affect files handled that go through PHP, which means HTML files are not included, unless your server is set up to pass HTML files through PHP. This can be done by adding the following line to your .htaccess:

    AddType application/x-httpd-php .html .htm
    
  • Auto prepending a file that generates output is a dangerous thing to do, because it will affect (break) scripts that set headers or use sessions.

NullUserException
A: 

Here's what you could do accomplish what you actually want.

  • Create two files, called prepend.php and append.php.
  • Use the method previously described to set them as auto_prepend_file and auto_append_file.

prepend.php

<?php

function callback($buffer) {
    $fileContents = file_get_contents('somefile.html');

    // insert $fileContents right BEFORE </head> tag
    return str_replace('</head>', $fileContents . '</head>', $buffer);

    // insert $fileContents right AFTER <body> tag
    // return str_replace('<body>', . '<body>' . $fileContents , $buffer);

}

ob_start("callback");

?>

append.php

<?php

ob_end_flush();

?>

This relies on output buffering and is relatively expensive. You might also run into problems if your scripts also use output buffering.

So my best advice is to design your scripts in a way that the a layout is used for every page and you can easily change what's included in your pages by just changing the layout.

NullUserException
Hi. I have no idea what output buffering is or if my scripts use that... thanks for this prepend.php script. How could I modify it to put the code AFTER a specific string like "<body">, and can you explain what ob_start("callback"); and ob_end_flush(); are for? Thanks!
RB. J
@RB.J `ob_start` turns on output buffering, `ob_end_flush()` sends the buffered output and turns it off. For your other question, I've updated my answer - just switch the commented lines. Inserting after `<body>` will be a bit more unreliable because the tag might contain attributes.
NullUserException
A: 

In htaccess you can put

php_value include_path "your/include/path/here/googleanayltics.php"
JapanPro