Hướng dẫn php auto_prepend_file not working

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.

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 , entire contents of the tag, the opening tag for then the header content. Then the footer file will contain the footer content, closing tag and closing 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.

Background: I've got some code that checks to see if a user has a valid session before processing the php page that I would like to set as the auto_prepend_file. However, I need to exclude the page where the user attempts to login from requiring a valid session. I would like to be able to set the auto_prepend_file value on an per directory basis.

Environment: PHP 5.2.6m Apache 2, running php as a cgi not as mod_php, on Windows [if that matters] and on a machine that I have complete control over [not a hosted environment]

Using a htaccess file is out b/c I am not using mod_php. I have not been able to alter the in php.ini to set the auto_prepend_file, the server throws an internal error. And ini_set[] does not work b/c it has already loaded the session checking file before I can change the value of auto_prepend_file.

I do not see a way to set auto_prepend_file on a per directory basis if you are not using mod_php/htaccess. Am I missing something?

asked Nov 22, 2010 at 17:16

CLJCLJ

1,8375 gold badges22 silver badges35 bronze badges

2

You mention you can't use .htaccess files but can you make modifications to httpd.conf and do something like this:


    Php_value auto_prepend_file c:/wamp/www/dev/prepend.php

EDIT - just realised this doesnt work when running as CGI. I think though that one thing that will work is if you create a copy of your php.ini file, place it in the directory, and put your prepend directive in there like:

auto_prepend_file = c:/wamp/www/dev/prepend.php

answered Nov 22, 2010 at 18:24

robjmillsrobjmills

18.2k15 gold badges74 silver badges120 bronze badges

3

answered Nov 22, 2010 at 18:28

MatthewMatthew

46.8k11 gold badges85 silver badges97 bronze badges

2

There is a great tutorial called: Automatically Include Files with PHP & Apache that explain how to do that with the apache directive and PHP code to append at the end. First, define a file to catch the page before it's outputted and append whatever you want:


Then add the Apache directive to your virtual host. You need to prepend the PHP file in order to use the ob_start[] method :


    AddType application/x-httpd-php .html .htm
    php_value auto_prepend_file /absolute/path/to/apache-prepend.php

answered Jan 25, 2011 at 21:38

Édouard LopezÉdouard Lopez

37.3k25 gold badges118 silver badges171 bronze badges

Not the answer you're looking for? Browse other questions tagged php apache .htaccess apache2 or ask your own question.

Chủ Đề