Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute php script before every php script?

Tags:

php

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.

like image 401
Will Avatar asked Oct 01 '10 17:10

Will


People also ask

How can I run one PHP file from another PHP file?

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

What is the order of correct execution for a PHP program?

When PHP reads a file, it compiles it to bytecode (compile time), then executes it (execution time / runtime). Unconditional function declarations are read at compile time, so that functions are already known when your code is executed.


2 Answers

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
like image 127
Matthew Avatar answered Oct 12 '22 03:10

Matthew


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.

like image 21
Pekka Avatar answered Oct 12 '22 02:10

Pekka