WojonsTech
WojonsTech

Reputation: 1277

PHP auto include

I want to auto include a PHP script onto every exection into the server, I was hopping to do it via the PHP ini, via a setting or being able to write an extention in php that was simple and included my php script.

Upvotes: 11

Views: 14063

Answers (3)

jbnunn
jbnunn

Reputation: 6355

Another solution, if you're on Apache and this is available to you, is to use .htaccess. I add a 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 in /var/www/mysite.com/config/. Admittedly, I've done this without knowing the auto-prepend solution--which looks much cleaner and more efficient.

Upvotes: 3

Kyle Hudson
Kyle Hudson

Reputation: 898

If running Apache and can access .htaccess you can do the following 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 prepend prepend.php and append append.php using the instructions below, when main.php is called the following would be outputted 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 prepend prepend.php and append append.php using the instructions below, when main.php is called the following would be outputted from the script:

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

Prepending a script

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

php_value auto_prepend_file prepend.php

A path does not need to be included but if it isn't then it will use the include path to find it. This can result in an error like "Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0" if there is no copy of this file in the include path or directory the script is in. So it's best to have the full path to the prepended file. Appending a script

This is almost the same as prepending a script and the same notes apply. The way to append a file is as follows:

php_value auto_append_file append.php

Overriding a setting so nothing is prepended or appended

If you need to override an existing auto_append_file or auto_prepend_file setting you can do this by setting the value to "none" like so:

php_value auto_prepend_file none
php_value auto_append_file none

This can be useful if you want to have .htaccess set the append/prepend file at the root level of a website but then want a particular subdirectory to not do the append or prepend. You would create a new .htaccess file in that subdirectory which sets them to none as above.

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

Upvotes: 4

Lock
Lock

Reputation: 5522

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

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

Upvotes: 20

Related Questions