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.
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
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/
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.
Related
I'd like to insert a line, similar to include('path to php file.php'), into php.ini.
Actually I already found this solution from the Internet, however I don't remember which parameter is used in php.ini in this case.
For what: I want to execute a short code (written by php) before every php script from my web server.
If I understand you correctly, you're looking to run a PHP script before all files on your server? If so, the auto-prepend-file directive in php.ini configuration is perfect for this. From the help docs:
Specifies the name of a file that is automatically parsed before the
main file. The file is included as if it was called with the require
function, so include_path is used.
The special value none disables auto-prepending.
Sample Code:
# Inside either main php.ini or child file
auto_prepend_file=/path/to/your/global/php/file.php
Note that PHP also gives the ability to append a file after every script interpretation as well. There is also a previous SO entry for this with additional information. HTH.
You must be searching for auto_prepend_file or auto_append_file, i.e.:
auto_prepend_file string
Specifies the name of a file that is automatically parsed before the
main file. The file is included as if it was called with the require
function, so include_path is used.
auto_append_file string
Specifies the name of a file that is automatically parsed after the
main file. The file is included as if it was called with the require
function, so include_path is used.
Usage php.ini:
auto_prepend_file="/path/to/prepend.php"
auto_append_file="/path/to/append.php"
I've always placed
php_value auto_prepend_file [path to config.php]
in the .htaccess file to include my config.php file in every page. However, I just switched to a new host (namecheap), and this host gives me the error "Invalid command 'php_value', perhaps misspelled or defined by a module not included in the server configuration".
According to this answer, the host is forcing me to set the auto_prepend file directly in the php.ini. I did this (placed a php.ini into public_html and set the auto_prepend_file value), and it worked, but only for the home page. In every page besides the home page, the config.php is not included. It seems like the auto prepend only works on every page if I paste the php.ini into every directory, which definitely isn't proper. I'm at a loss how to include the config.php into every php file... I really don't want to paste a require_once command into every file.
Found the answer here.
I had to add suPHP_ConfigPath /home/username/public_html to make the php.ini file recursive instead of setting php_value.
I built a website using PHP 5.3 (with Apache 2.4).
I have global file that needs to run before every page (to set timezone and more global settings).
So in each file I must to remember to include it in the top of the page. It is not convenient.
Is there any way to set a boot file that runs automatically before every file?
For example you can use this php config directive:
http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file
auto_prepend_file string
Specifies the name of a file that is automatically parsed before the
main file. The file is included as if it was called with the require
function, so include_path is used.
I have a 'template.php' file which I manually include in most of my php files - is there a way to somehow automatically include a php file into all of my other files?
I could not find any examples that worked for me.
You can add an .htaccess file to your directory and use the following
php_value auto_prepend_file template.php
You can also read about PHP's auto_prepend_file directive Here
How would I run this before every php script besides putting it in all of them?
if ($_SERVER['REMOTE_ADDR'] == '123.123.123.123')
{
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];
}
I basically want the same affect as putting that at the top of every script without actually doing that.
Put it in its own file and set the auto_prepend_file configuration in the php.ini / .htaccess file to point to it.
Update: Since you mentioned lighttpd in a comment, note that you can configure it like this in the global INI file with PHP 5.3:
[PATH=/vhost/domain.com]
auto_prepend_file = /vhost/domain.com/foo.php
[HOST=domain.com]
auto_prepend_file = /vhost/domain.com/foo.php
Or you can create the file /vhost/domain.com/.user.ini and do the same:
auto_prepend_file = /vhost/domain.com/foo.php
If you have the necessary rights to change your PHP configuration, auto_prepend_file is exactly what you're looking for.
auto_prepend_file
Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require() function, so include_path is used.
The special value none disables auto-prepending.