Powered By Blogger

Monday, 8 August 2011

auto_prepend_file and auto_append_file

SSI is great, no doubt. But, if you just need to implement a process for including a header and / or a footer in your pages, there are other options – one such option being PHP. When most people think of modularising their pages with PHP they think of the include() construct – use of this feature has, in my opinion, the same draw backs of using Server Side Includes: you have to manually add an include() statement to every page. However, the PHP language does offer another alternative through its initialisation file (the php.ini file) by way of the auto_prepend_file and auto_append_file values.
Each of these values define an absolute path to a PHP file and when set the auto_prepend_file value will include the indicated file at the beginning of every PHP file that is executed on the server. Likewise, the file indicated in the auto_append_file value is added to the end of each executed PHP file.
Keep in mind that this process isn’t without its drawbacks – mainly in Lighttpd and Nginx where PHP configuration is server-wide. In these environments the two files specified in your php.ini file will become part of every PHP file that is executed on the server. In an Apache environment, however, where PHP has been installed as an Apache Module (or, indeed, in the drop-in replacement server, Litespeed), PHP initialisation values can be defined on a per-directory level and this opens up a world of possibilities.

Setting the Values Per-Directory

Please note: If PHP isn’t installed as an Apache Module (e.g. PHP installed through FastCGI) then this process won’t work and any PHP configuration will have to be done server-wide or per PHP file at runtime.
How you define these values will depend on the type of Apache environment in which you’re operating. In most circumstances the values are defined in a .htaccess file – the standard Apache directory configuration file. However, if your server has been patched with suPHP or PhpSuExec the you will need to include the values in a per-directory php.ini file. If you define the values in a .htaccess file and are greeted with a server error then you need to use the latter method.

Setting the Values in a .htaccess File

The syntax for defining a PHP initialisation value in a .htaccess file is: #A Comment
php_value setting_name setting_value

So, to set the auto_prepend_file and auto_append_file values you would add the following lines to a directory’s .htaccess file (changing the file path to your own, of course): #Prepend to each PHP File
php_value auto_prepend_file /home/path/to/header.php

#Append to each PHP File
php_value auto_append_file /home/path/to/footer.php

Setting the Values in a php.ini File

IMPORTANT: To use a per-directory php.ini file you do NOT need to include a full php.ini file. Simply create a php.ini file in the desired directory and include the value you wish to define only. The PHP installation’s default php.ini file will take care of all other values.
The syntax for defining an initialisation value in a per-directory php.ini file where the value is a string is: setting_name = "Setting Value"
So, to set the auto_prepend_file and auto_append_file values you would add the following lines to a directory’s php.ini file (changing the file path to your own, of course): auto_prepend_file = "/home/path/to/header.php"
auto_append_file = "/home/path/to/footer.php"

No comments:

Post a Comment