PHP auto include

I want the car to include a PHP script on each shutdown on the server, I jumped to do this through PHP ini, through setup or the ability to write an extension in php that was simple and included my PHP script.

+7
source share
3 answers

You can set the auto_prepend_file directive in the php.ini :

http://php.net/manual/en/ini.core.php#ini.auto-prepend-file

+17
source

If you are running Apache and can access .htaccess, you can otherwise look at @Lock's answer

 prepend.php: <?php echo "<p>this is the prepended file</p>\n"; main.php: <?php echo "<p>this is the main file</p>\n"; append.php: <?php echo "<p>this is the appended file</p>\n"; 

And add prepend.php and add append.php using the instructions below, when main.php is called, the following will be output from the script:

 <p>this is the prepended file</p> <p>this is the main file</p> <p>this is the appended file</p> 

And add prepend.php and add append.php using the instructions below, when main.php is called, the following will be output from the script:

 <p>this is the prepended file</p> <p>this is the main file</p> <p>this is the appended file</p> 

Preparation script

To add a file so that it is parsed before the main script, add the following parameter to the .htaccess file, php.ini (which, of course, will affect all websites) or config:

 php_value auto_prepend_file prepend.php 

The path does not need to be included, but if it is not, it will use the include path to find it. This can lead to the error "Warning: Unknown: Could not open stream: there is no such file or directory in Unknown on line 0" if there is no copy of this directory or the directory in which the script is located in this path. it is best to have the full path to the previously added file. Adding a script

This is pretty much the same as adding a script and the same notes apply. The way to add a file is as follows:

 php_value auto_append_file append.php 

Cancel setting, so nothing added or added

If you need to override the existing auto_append_file or auto_prepend_file parameter, you can do this by setting the value to none as follows:

 php_value auto_prepend_file none php_value auto_append_file none 

This can be useful if you want .htaccess to install the append / prepend file at the root level of the website, but then you want some particular subdirectory not to add or add. You will create a new .htaccess file in this subdirectory that does not install them as described above.

Source: http://www.electrictoolbox.com/php-automatically-append-prepend/

+4
source

Another solution, if you are on Apache, and this is available to you, is to use .htaccess . I add the line:

php_value include_path "/var/www/mysite.com/config"

and then in my PHP files I can include

include_once('someconfig.php');

which looks at /var/www/mysite.com/config/ . Admittedly, I did this without knowing how to use an automatic preliminary solution that looks much cleaner and more efficient.

+3
source

All Articles