I'm trying to create a PHP script that will always be called on every page load, without altering existing code of a website, and as little extra configurations as possible.
Here's my problem, I do not want it to effect anything regarding the actual requests (headers). Here is the desired flow:
Request-> somescript.php -> Actual destination
So, I cannot use the header() function, and the solution has to be completely server side.
I was thinking, after the script completes it's task, it could include/require the PHP file that was intended.
Any ideas/suggestions would be really appreciated.
Regards,
Branden Martin
PS: I cannot use php_value inside of the .htaccess file as will sometimes be used in a CGI environment.
Bottom line: Need some code to run on every server request
You want a script to be executed before any other scripts? If you uses Apache I can think of one possible solution... if you can rewrite in .htaccess
...
.htaccess
:
RewriteEngine On
RewriteBase /test
RewriteRule !(common.php) common.php/$1
common.php
:
<?php
$request=substr($_SERVER['REQUEST_URI'],6);
if($request==""){
$request="index.php";
}
if(substr($request,0,1)=="/"){
// Prevent browsing local files (*nix)
header("HTTP/1.1 403 Forbidden",true,403);
}else if(!file_exists($request)){
// Not found
header("HTTP/1.1 404 Not Found",true,404);
}else{
// Request URI should be valid
do_what_you_want_to_do_before_anything_else();
// Include the original request URI...
if(substr($request,-4)==".php"){
include $request;
}else{
// Prevent executing non-PHP code
echo file_get_contents($request);
}
}
?>
There is still much code to write, including security measures and changing the $_SERVER
array if the child scripts cares.
If you can modify the servers' php.ini perhaps you can use the auto_prepend_file directive?
You need to be careful though: If you add it directly to the .ini it is active for all sites hosted by that webserver. You can also add it via the php_value in apache or php-fpm to do it on a per site bases, or you can build in a site detection in your script.
In terms of of easiness in configuring it, you can't get it much simpler.