How to Start Sessions in PHP with a Prepended File - dummies

How to Start Sessions in PHP with a Prepended File

By Steve Suehring, Janet Valade

In order to use sessions, you need to call the session_start() function on every page that will use sessions. This can be cumbersome, especially if you’re trying to tack sessions onto several PHP programs. You can use an auto_prepend_file to call session_start and, in doing so, you don’t have to change any other files!

In the following exercise, you create two files: one that will be the main file and another containing a prepended function to start a session. Prior to performing this exercise, you should ensure that .htaccess files work or that you can alter your Apache web server configuration.

Be sure to restart Apache if you make a change to the configuration.

Within the .htaccess file for your document root, place the following code:

php_value auto_prepend_file "prepend.php"

Alternatively, you can add that line within the <Directory> stanza in the Apache configuration for your web server for your document root. For example, if your document root is /var/www you can add that line after the <Directory /var/www> directive and before the closing </Directory> line in the Apache config.

Open your text editor and create a new empty file. Within the file, place the following code:

if (isset($_SESSION)) {
    print "Session has started!";
} else {
    print "Session has not started";

Save the file as session.php within your document root.

Open a web browser and point to http://localhost/session.php. You should see a page like this one.


Minimize the web browser and create a new file within your text editor. Within that file place the following code:


Save the file as prepend.php in your document root.

Within your web browser, reload the session.php file or go to http://localhost/session.php to view the session.php file you created earlier. You should now see a page like this one.


If you receive a blank page or an error displayed through the browser, then the prepended file wasn’t found. Check the simple stuff, like spelling of the file (prepend.php). Also check to make sure that the file you called with the auto_prepend_file directive is where it should be, in the document root if that’s how your web server is configured.

If you receive a page that still says “Session has not started,” then there’s a chance that Apache isn’t seeing your auto_prepend_file directive at all. If you’ve placed it in an .htaccess file in your document root, you need to make sure that Apache is reading the .htaccess file. Continue reading or check with your hosting provider to see if .htaccess files are allowed.

Some web server configurations don’t allow for .htaccess files. You can reconfigure Apache to allow them by changing the AllowOverride directive to All for the directory from which you want to read the .htaccess file (in this case, your document root). The directive should look like this:

AllowOverride All

Prepended files can be incredible helpers, but they also can sometimes cause confusion. For example, if you aren’t sure why a program is doing something, an auto-prepended file can sometimes add to that confusion because it loads so many other files and functions — adding ample room for error.

Additionally, every request must now use that auto-prepended file, which can cause performance issues if you chain too many required and included files from that prepended file. With that said, the benefits usually outweigh the drawbacks for prepended files.