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.
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 want to set my own (php2.ini) file, with just a few lines that must overwrite the first php.ini settings - only for those lines.
So I start looking on google and it seems like what I need is the parse_ini_file function.
Ok. Now, in index.php I have:
parse_ini_file("php2.ini");
phpinfo();
exit();
But those settings from php2.ini are not loaded...
Trying to see if the file php2.ini is read-it and it is, beacause if I set parse_ini_file("php2-not-exist.ini"), it tells me that the file cannot be found. So the file php2.ini is read, but the settings are not overwritten.
The php2.ini file has inside the next lines:
error_log = "errors.log"
display_errors = "0"
max_execution_time = "300"
upload_max_filesize = "60M"
post_max_size = "60M"
date.timezone = "Europe/Bucharest"
default_charset = "UTF-8"
mbstring.internal_encoding = "UTF-8"
mbstring.http_output = "UTF-8"
mbstring.encoding_translation = "On"
mbstring.func_overload = "6"
Trying to let this file with just one line like post_max_size = "60M", but still not overwriting the general php.ini settings.
What am I missing here?
I know that some settings can be setted only and only using the php.ini file configuration. So setting a second file with parse_ini_file witch is a function from php... something is not correct, because some servers doesn't allow you to edit the general php.ini file. So how can the php let you set a second php ini file if you don't have access to the general one ? I don't know if that has something to do with my problem. I'm just saying that maybe here is the problem.
Right now I'm using wamp with php 5.5.12. I just want when I upload my website up, to be all done. I know that some hosting servers lets you to have a second php.ini file that must be uploaded in public_html or whatever folder it is. This is why I want to set my php2.ini file. To have the website clean when I'll upload to hosting.
Thank you.
The php parse_ini_file() function reads an ini file into an associative array. It does not set them up as settings like the php.ini file.
There is not a convenience function to override the server settings like this. What you may want here are .user.ini (the leading dot is important) files:
Since PHP 5.3.0, PHP includes support for configuration INI files on a per-directory basis.
and:
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']). In case the PHP file is outside the document root, only its directory is scanned.
For full documentation on that feature, take a look at http://php.net/manual/en/configuration.file.per-user.php.
Note that only a subset of settings can be changed here:
Only INI settings with the modes PHP_INI_PERDIR and PHP_INI_USER will be recognized in .user.ini-style INI files.
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.
Is there anything I can change in php.ini that includes a php file before running any other file? The equivalent of adding "require('somefile.php');" before every file?
You can use the auto_prepend_file directive.
Search for auto_prepend_file = [path/to/file] in php.ini. Replace [path/to/file] with your file
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.