Here\'s a problem that I\'ve been running into lately - a misconfigured apache on a webhost. This means that all scripts that rely on $_SERVER[\'DOCUMENT_ROOT\']
There's no need to modify all scripts.
You can run PHP file before any script is run using auto_prepend_file.
$_SERVER
is just an array, you can modify it and set correct $_SERVER['DOCUMENT_ROOT']
.
PHP should be setting the current directory to the one the script is in, so as long as that's not broken you should be able to figure out the document root using $_SERVER['SCRIPT_FILENAME']
and getcwd()
. (I can't remember all the $_SERVER vars off the top of my head, there might be something in phpinfo() that's more useful.)
Based on http://www.helicron.net/php/:
$localpath=getenv("SCRIPT_NAME");
$absolutepath=getenv("SCRIPT_FILENAME");
$_SERVER['DOCUMENT_ROOT']=substr($absolutepath,0,strpos($absolutepath,$localpath));
I had to change the basename/realpath trick because it returned an empty string on my host. Instead, I use SCRIPT_FILENAME
. This probably won't work on IIS anymore (but the original scripts that used the $_SERVER variable probably wouldn't either).
Why not demand that your webhost configures his servers correctly?
these kind of things tend to linger silently in your code and never get removed (but still active) until someone finally fixes the server. Thén everything will break again.
Or, move your stuff to a host that will work. If this is broken, who knows what you will find next.
This is one reason why people siphon everything through a bootstrap /index.php using htaccess and/or query strings. You can use the dirname( __FILE__ )
trick noted above and get the public base of your app that way.
If you're too far into it to switch to a single entry point, one thing I've seen people do is have a common header to their script which walks up the directory tree to find a file which is unique to the base dir:
function findAppBase( $dir ) {
if( file_exists( "$dir/unique_file.txt" ) ) {
return $dir;
return findAppBase( dirname( $dir ) );
}
$base = findAppBase( dirname( __FILE__ ) );
That code hasn't been tested, and there might be a slicker way using the vars in $_ENV
or $_SERVER
that will do what you want...
In PHP5 there is the magic constant __FILE__
that contains the absolute path of the file in which it appears. You can use it in combination with dirname to calculate the document root.
You can put a statement like the following one in a config file
define ('DOCUMENT_ROOT', dirname(__FILE__));
this should do the trick