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.
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 have a file called init.php which I want to get automatically included in each and every HTTP request coming to my server. My server is on LAMP configuration with PHP 5.3 and fast CGI. Any method to achieve this is welcome.
What I have already tried:
I have already tried the auto_prepend_file method with .htaccess file but no success.
I have done the following.
.htaccess File:
php_value auto_prepend_file /home/user/domain.com/init.php
init.php file:
<?php
echo "statement 2";
?>
index.php file:
statement 1
So, now if I visit http://domain.com/ I find only statement 1 getting printed. statement 2 is not getting printed.
Kindly let me know how to correct this or if there is any other way to achieve this.
My server is on LAMP configuration with PHP 5.3 and fast CGI.
You can not set PHP ini directives with the .htaccess method with FCGI/CGI. You need to apply the changes within the php.ini or better the user ini files Docs instead.
Place a file called .user.ini into the document root and add the auto_prepend_fileDocs directive in there:
auto_prepend_file = /home/user/domain.com/init.php
This should do the job for you.
My server is on LAMP configuration with PHP 5.3 and fast CGI.
fast CGI means no .htaccess directive would work. Change your php.ini instead.
automatically included in each and every HTTP request
with auto_prepend_file you obviously can't include a php file to "each and every HTTP request", but to requests routed to PHP files only.
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.
How do I include a php.ini file in another php.ini file?
I don't think you can "include" .ini files from the main php.ini file.
One possible solution, though, might be to use this option on the configure line, when compiling PHP:
--with-config-file-scan-dir=PATH
Set the path where to scan for configuration files
If this option is used at compile-time, PHP will look for every .ini file in this directory, in addition to the "normal" php.ini file.
I suppose this is what is used by Ubuntu, for instance, which uses a different .ini file for each downloaded extension, instead of modifying php.ini.
The path to the php.ini file is being defined with this option, on the configure line:
--with-config-file-path=PATH
Set the path in which to look for php.ini [PREFIX/lib]
Still, it probably means you'll have to re-compile PHP -- which is not that hard, btw -- the hardest part being to get the dependencies you need.
And, here is a post on the internals# mailling-list that says the same thing as I do: config files and PHP_CONFIG_FILE_SCAN_DIR
One can also define the path in ~/.bashrc
export PHP_INI_SCAN_DIR=/usr/local/lib/php.d
I installed Memcached for php and wasn't sure how to make sure that its ini was included in my php.ini file, but as it turns out, it automatically is. You can validate what is being loaded by running php --ini.
php --ini
Configuration File (php.ini) Path: /opt/local/etc/php5
Loaded Configuration File: (none)
Scan for additional .ini files in: /opt/local/var/db/php5
Additional .ini files parsed: /opt/local/var/db/php5/memcached.ini
EDIT: My answer was mistaken. This only works in .conf files, which is not the question asked. Better testing showed that it won't work in php.ini files, where include statement is ignored.
I just tested it on DebianĀ 9 (Stretch) with PHP-FPM. From some .conf file, use this syntax:
include=/path/to/special-php.ini
or even
include=/path/to/special-dir-full-of-conf-files/*.conf
as it is used in
/etc/php/7.0/fpm/php-fpm.conf
include=/etc/php/7.0/fpm/pool.d/*.conf
By the way, this will be most useful if you split your settings by topic, and or if you want a set for development and another one for production. Then you could do it the Debian/Apache style like
/etc/php/conf-available/
/etc/php/conf-enabled/
with symliks from the second to the other and an include to that one.
It seems you cannot include one ini file into another so it gets referenced and loaded. But you can set php up to load several files by telling it which folders to look into.
When using a FastCGI setup (possibly in FPM, too, though I don't know that for sure) you can export environment variables from within the PHP wrapper.
There you could do:
export PHP_INI_SCAN_DIR=/etc/php5/cgi/conf.d:/var/www/mydomain.net/etc
/var/www/mydomain.net/etc is just an example. It's the folder where you put your additional ini files into. It seems this can be a : separated list.
Use a phpinfo.php (file called arbitrarily containing only <?php phpinfo();), open the corresponding URL in your browser and check the list of directories that are parsed and the list of files that get loaded in the top area of it.
/etc/php5/cgi/conf.d should always be included (I guess because it was compiled into the PHP executable) and possibly not really be needed.
You can't. Read online pages:
The configuration file
SUMMARY: The configuration file
(php.ini) is read when PHP starts up.
For the server module versions of PHP,
this happens only once when the web
server is started. For the CGI and CLI
version, it happens on every
invocation.
.user.ini files
SUMMARY: In addition to the main
php.ini file, PHP scans for INI files
in each directory, starting with the
directory of the requested PHP file,
and working its way up to the current
document root (as set in
$_SERVER['DOCUMENT_ROOT']). Only INI
settings with the modes PHP_INI_PERDIR
and PHP_INI_USER will be recognized in
.user.ini-style INI files.
You could try to simulate it making use of the ini_set function. But as the "man page" indicates, not all ini options can be changed using ini_set. It's definitely a useful function, though.