11

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.

6
  • 6
    You're looking for the auto_prepend_file option. Commented Dec 26, 2012 at 0:18
  • You had the right answer if you posted it at the bottom you would have been given the point Commented Dec 26, 2012 at 0:41
  • 1
    possible duplicate of How to include a file automatically in PHP? Commented Dec 26, 2012 at 2:31
  • 1
    how is this off topic?? Commented Jan 5, 2015 at 15:51
  • 1
    This is not off topic. Especially if it is a possible duplicate of another question which is not regarded as off topic. Commented Mar 9, 2019 at 22:06

3 Answers 3

20

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

Sign up to request clarification or add additional context in comments.

1 Comment

works like a charm i did not know this existed never seen it used before.
4

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/

1 Comment

I was thinking of something like this before but it is for clients that could be on apache or nginx or anything that they pick.
3

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.

1 Comment

this was also my first thought but i did not want to have to add that line to all files i was hoping it would be global so no mods to scripts need to happen just to the server level

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.