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)
You can append or prepend files by setting a PHP ini value in .htaccess
(It'd be better to set them manually in php.ini)
http://php.net/manual/en/ini.core.php
In htaccess you can put
Here's what you could do accomplish what you actually want.
auto_prepend_file
andauto_append_file
.prepend.php
append.php
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.
There are two
php.ini
settings you might be interested in:auto_prepend_file
andauto_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:
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.
if its in the same folder
If not use this