Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
PHP ›
PHP auto_prepend_file and auto_append_file

PHP auto_prepend_file and auto_append_file

August 7, 2011 PHP Jesin A 12 Comments

php category thumbnail

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.

Related posts:

php category thumbnailHow to use PHP to minify HTML output php category thumbnailPHP Captcha Tutorial windows category thumbnailWindows Logon and Logoff Scripts php category thumbnailPHP Sessions Tutorial remove w3 total cache commentW3 Total Cache Remove Footer

Tags: php.ini

Comments

  1. mike says

    June 8, 2013 at 7:33 am

    they DO work in htaccess… at least for me on an xampp server

    Reply
    • A.Jesin says

      June 9, 2013 at 2:48 pm

      Maybe on xampp but when I tried on a shared hosting it didn’t work for me.

      Reply
  2. Daoud says

    September 28, 2013 at 3:18 pm

    Thanks, it’s very useful.

    Reply
  3. Vlad says

    November 2, 2013 at 3:49 pm

    How can you disable the auto_prepend_file for one specific file?

    Reply
    • Jesin A says

      November 2, 2013 at 4:16 pm

      Hi Vlad,

      You have to add this to the start of that file.

      ini_set("auto_prepend_file","none");

      Read more about ini_set()

      Reply
      • Vlad says

        November 6, 2013 at 4:10 am

        Thanks!

        Reply
  4. Fabio says

    November 2, 2014 at 5:42 am

    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

    Reply
    • Fabio says

      November 2, 2014 at 5:43 am

      alguem sabe arrumar isso?
      queria instalar o swift panel

      Reply
      • Jesin A says

        November 7, 2014 at 3:52 pm

        I do not understand Portuguese, with a translator I understand that you’re using swift panel and receive the following error:

        The encoded file /var/www/html/upgamesbrasil.com/web/Swift_Panel/upgames/install/index.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

        Place the following lines in your .htaccess file:

        php_value auto_prepend_file none
        php_value auto_append_file none
        Reply
  5. Willian says

    November 26, 2020 at 7:46 am

    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

    Reply
  6. Farshid Najafi says

    January 30, 2021 at 9:16 pm

    Can this feature be used only for files with a specific name?
    for example all “wp-config.php” files in a shared hosting server.

    Reply
    • Jesin A says

      January 30, 2021 at 11:34 pm

      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"; use require $v; then load this file using auto_prepend_file.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.