Run a php script before every php script?

How can 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']; } 

Basically, I want the same effect to affect the fact that the top of each script is not executed.

+6
php
source share
2 answers

Put it in your 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 a 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 
+10
source share

If you have the necessary permissions to modify your PHP configuration, auto_prepend_file is exactly what you are 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 were called using the require () function, so include_path is used.

The special value none disables automatic addition.

+1
source share

All Articles