This article will show you how to use the PHP configuration directives auto_prepend_file and auto_append_file. These two PHP directives perform the same function as require() but they do it globally on all PHP scripts. The PHP auto_prepend_file and auto_append_file directives can only be used in php.ini files. They do NOT work when used in .htaccess file, I’m mentioning this so that you don’t waste precious time editing your .htaccess file. If you want to set these configuration directives on directory basis you can use them in your custom php.ini file.
The purpose of these PHP configuration directives is to execute a specified file before or after any PHP script stored on your computer/server. This is similar to using a require() function at the beginning or end of a PHP script. The auto_prepend_file directive parses a file before executing any PHP file, so this can be extremely useful if you have a set of user defined PHP functions which need to be called in all PHP scripts.
I’ll explain this scenario, first lets say you’ve created two functions for addition and subtraction.
<?php function add($a,$b) { return ($a+$b); } function subtract($a,$b) { return($a-$b); } ?>
Save the code in a separate file myfunctions.php. Now edit the php.ini file, this file is located in /etc/php.ini in Linux, C:\PHP\php.ini in Windows, C:\wamp\bin\php\php.ini in WAMP and C:\xampp\php\php.ini in XAMPP. Find and edit the following line
auto_prepend_file = "Location to myfunction.php"
If you’re editing a custom php.ini file you should manually add this line. The location you specify should be an absolute location like /var/www/html/myfunction.php windows users should use forward slash in their file path C:/wamp/www/myfunction.php
Now all you need to do is to call this function in any PHP script you code. The usage of auto_append_file directive is also similar, the only difference is the file specified is parsed AFTER a PHP script executes.
The way in which these configuration directives work might make you think you can use these for adding a header and footer all pages, if you plan to do so remember that while execution the files are merely prepended and appended to the PHP file requested hence you should code in such a way that the header file has the just opening tag for <html>, entire contents of the <head> tag, the opening tag for <body> then the header content. Then the footer file will contain the footer content, closing </body> tag and closing </html> tag in that order. So the main file you code should only have the content that comes after the header contents and before the footer contents. Did you notice how cumbersome it is when used like this ? So it is best you use the auto_prepend_file and auto_append_file directives for including scripts that don’t generate an output viewable on the browser like visitor tracking scripts, user definitions of functions like the example listed above.
mike says
they DO work in htaccess… at least for me on an xampp server
A.Jesin says
Maybe on xampp but when I tried on a shared hosting it didn’t work for me.
Daoud says
Thanks, it’s very useful.
Vlad says
How can you disable the auto_prepend_file for one specific file?
Jesin A says
Hi Vlad,
You have to add this to the start of that file.
Read more about ini_set()
Vlad says
Thanks!
Fabio says
meu servidor da esse erro
O arquivo codificado /var/www/html/upgamesbrasil.com/web/Swift_Panel/upgames/install/index.php não pode ser executado porque a configuração php.ini auto_prepend_file ou auto_append_file está em uso. Por favor, desativar essas configurações, ou entre em contato com o provedor do script. em Unknown on-line 0
Fabio says
alguem sabe arrumar isso?
queria instalar o swift panel
Jesin A says
I do not understand Portuguese, with a translator I understand that you’re using swift panel and receive the following error:
Place the following lines in your .htaccess file:
Willian says
Hello Jesin, I have a similar error on my website could you help me with this?
Fatal error:
The encoded file /var/www/html/muoffensive.com/web/system/common.php cannot be run because the php.ini setting auto_prepend_file or auto_append_file is in use. Please disable these settings, or contact the provider of the script. in Unknown on line 0
Farshid Najafi says
Can this feature be used only for files with a specific name?
for example all “wp-config.php” files in a shared hosting server.
Jesin A says
You’ll have to create a “loader” file that searches for files and includes them. Something like this – https://sathyalog.wordpress.com/2014/02/13/search-files-in-folders-and-sub-folders-in-php-using-glob/
Instead of
print "$v";
userequire $v;
then load this file using auto_prepend_file.