Automatically append and prepend a php file to current script using htaccess
It is best practice to add common file to every php script in our application ,such as libraries cleaning input variables from $_GET,$_POST,$_COOKIE,cleaning or modifying the output of the php script by providing plug in functionality.
We know best example for this many free hosting providers use this settings. they append a advertisement script to our files while it output buffering.
here we learn how to add or remove append or prepend files using htaccess. to continue this tutorial we must have the knowledge of htaccess of apache and basic settings in php.ini
add files to script
this can be achieved by auto_append_file and auto_prepend_file php configuration options.These options can be set in the PHP config, virtual host settings or in a .htaccess file. This post shows how to set these and how to override a setting so no file is appended or prepended.
Example
We have three files append.php, mainscript.php, prepend.php and htaccess file to achieve this.
<?php //prepend.php echo "<p>this is the prepended file</p>\n"; ?>
<?php //mainscript.php echo "<p>this is the main file</p>\n"; ?>
<?php //append.php echo "<p>this is the appended file</p>\n"; ?>
Out put of our mainscript file
<p>this is the prepended file</p> <p>this is the main file</p> <p>this is the appended file</p>
Set up php values in htaccess
prepending a file
php_value auto_prepend_file prepend.php
Append a file
php_value auto_prepend_file append.php
Removing or modify existing values
we can override these values for each php file or each directory in our application. we can remove existing auto_append ,auto_prepend values by setting its values to none.
php_value auto_append_file none php_value auto_prepend_file none
