Automatically Include Files with PHP & Apache
Need to include a files in all of your site’s pages? Normally, you’d use require() or include(). But do you really want to manually past the include code in every page?
There’s the little known PHP directive auto_prepend_file and auto_append_file. What are PHP directives? They’re essential global PHP settings that are defined in you php.ini file. There is surprisingly little information about these methods out there, but luckily they aren’t too difficult to work with.
The following method only works on Linux servers running Apache and PHP 4.2.3 and up.
Implementation
The best way to set PHP directives is through a .htaccess file. You should have one in your root directory (usually public_html or www). If not, go ahead and create one. ;-)
With the aid of Apache and an .htaccess file, you can automatically includes files to all files types that you specify. Here is the example code:
1 | php_value auto_prepend_file /home/username/public_html/admin/auto.php |
This will automatically prepend the file auto.php in all PHP, HTML, and HTM files. You want to be sure you use the absolute path to the file you are wanting to include. Relative paths won’t work here.
As is, the above script will only include the file in php files. To include any other files type you’d like, add the following before:
1 2 | AddType application/x-httpd-php .html .htm php_value auto_prepend_file /home/username/public_html/admin/auto.php |
This adds .html and .htm to the list.
Tips
A few things to keep in mind:
You can only use this method once per .htaccess file. This means, you can’t also write to include a file for different files types in the same file. There is only one auto_prepend_file directive that can be defined. You can add more prepends or appends in sub-directories as you’d like, but they override any others from its parent directories.
With the above in mind, you can also choose to exclude a directory from having the files included. Simply create another .htaccess file for that directory, and add:
1 | php_value auto_prepend_file none |
Why use this method instead of require() or include()? This method will include your selected file in ALL the file types you specify? Other reasons? If you want to compress all of your JavaScript files, you can set all .js files to be run through the PHP script to do so. Simple as that.
Other ways to use this method to your example is to adjust all your pages. With some fancy PHP coding, you can adjust all your <title> attributes to include your website name. Or you can add your google analytics code to each file above the </body> tag. You can edit the file as you see fit and change it however you’d like.
In theory, you can use this file as you template and insert the requested page into it…
ob_start()
You can use ob_start() to run each requested file through a function. What’s ob_start()? It’s an output buffering function that runs your pages output through the defined function before outputing. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | function template_filter($page) { $script = '<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E")); var pageTracker = _gat._getTracker("XX-XXXXXXX-X"); pageTracker._trackPageview(); </script>'; $replace = array('</body>','</BODY>'); $replace2 = array('</title>','</TITLE>'); $page = str_replace($replace, "{$script}\r</body>", $page); return str_replace($replace2, " - My Awesome Site</title>", $page); } ob_start("template_filter"); |
This code with run the requested page through the template_filter() function with the page’s contents passed as an argument (in this case, $page). The function then adds a Google Analytics code right before the </body> tag, and adds My Awesome Site to the end of the <title> tag.
You can do whatever adjustments you’d like with the ob_start() function. There are endless possibilities. Go find em.