Using PHP’s auto_prepend_file and auto_append_file

closeThis post was published 5 years 7 months 30 days ago, which is pretty much forever ago in internet time. As such, some of what's discussed here may no longer work as described.
TL;DR: FYI, YMMV
The PHP logo.

The PHP logo. (Photo credit: Wikipedia)

Do you find yourself writing the same code at the beginning or end of each PHP file? Are you always starting and closing sessions or database connections? Good developers are always looking for ways to do less work and achieve the same effect, so it seems like there has to be an easier way to take care of these repetitive tasks.

An Easier Way: auto_prepend_file and auto_append_file

PHP allows you to specify two files that execute before and after every page, respectively. Set this pair of core directives in your php.ini (they can also be set in an .htaccess file), and each points to a file with whatever PHP code you want to execute every page load.

The first, auto_prepend_file, defines a file to automatically always be executed before your other PHP code. The other directive, auto_append_file, similarly defines a file to automatically always be executed after your other PHP code.

What kind of code can you put into these files? Pretty much anything you want! Common commands such as session_start(), useful functions like password-protecting every page, or creating a template system to add a common header and footer to your whole site.

A Better Way?

Maybe these directives are an easier way, but are they a better way? Personally, I say, “probably not.”

I have read stories of developers whose applications rely heavily on auto_prepend_file and auto_append_file. While that’s convenient for them, it also makes those applications much less portable, and much harder to debug.

Imagine you’re given the task of fixing an application that someone else built. Some variable is set incorrectly, but despite all your searching, you can’t even figure out where it’s being set. You may not think to check for an auto_prepend_file in the php.ini (I certainly wouldn’t think to look there right away). Is it really that hard to add require('myfile.php'); to your files?

The real kicker for me is that you lose flexibility and future-proofing. When you use these directives, they’re called before every page. What if you have one or two pages where you don’t want the auto_prepend_file to run? If that’s not the case now, what about six months or a year down the line?

A Real World Example

With all that said, I actually do make use of auto_prepend_file on a daily basis. Where I work, we have two servers. One is our production server that runs all of our lives sites. The other is a development server, which “mirrors” our production server so we can test updates and upgrades without breaking our live sites. The problem is, because both servers host identical files, it’s often difficult to tell which server you’re looking at on the front end.

I’ve gotten around this by adding a simple line of text to an auto_append_file that appears at the end of my HTML file to let me know which site I’m viewing.

A Few Things to Keep in Mind

  • The file specified by auto_prepend_file and auto_append_file is included as if called by require(). This means that if PHP cannot find the file that you specified, it will throw a fatal error.
  • When setting these directives, use the full path to the files.
    • In Linux, this would be something like:  auto_prepend_file = "/var/www/html/myfile.php"
    • In Windows, this would be something like:  auto_prepend_file = "C:\inetpub\wwwroot\myfile.php"
  • It is possible to create a redirect loop by adding code to your auto_prepend_file, which, if a certain condition is not met, sends the user to a specific page, for example, login.php. The problem is, auto_prepend_file will be run when login.php tries to load, and since the user isn’t logged in, it will try to send them to login.php.
  • If your auto_prepend_file echoes or prints anything, you may have issues. In my tests, I set up my auto_prepend_file to echo a string, and then accessed a page with a header redirect. I expected the redirect to fail, but to my surprise it worked flawlessly. This doesn’t mean it couldn’t happen, though, so keep that in mind.
  • Along those same lines, if your files echo or print anything, that content will appear before and after every page. I stress this because it’s possible to have content outside of your HTML element, or before your DOCTYPE declaration. This will break your site in some browsers.
  • Setting these directives in your php.ini file will execute the same files for every site on your server. So be careful with that. To set it for a single site, set the directives in your .htaccess file for that site.

3 comments to Using PHP’s auto_prepend_file and auto_append_file

  • Papatrentecink  says:

    Thanks for all this information, very useful for a trainee like me. You should add the system you're working on, personnally I'm on IIS and I can't use a .htaccess file to auto_prepend to a specific website ( which has been a problem to test a second website in parallel ).

  • Thomas J. Brown  says:

    I'm on IIS as well, so I definitely feel your pain.

    As I mentioned in the article, these are best set in the php.ini file, which exists regardless of which system you're using.

    If you don't have access to that file, it's worth pointing out that many web hosts prohibit you from using auto_prepend_file in .htaccess as a security measure, so it may not work there either.

  • Kolo  says:

    auto_prepend_file is very useful for building very simple templating. Like for homepage.

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>