I have a class that is included in a bunch of other scripts in different directories. Occasionally, this class will need to issue a redirect to another file that is located in the same directory. I am having trouble getting this to work in a consistent manner.
For instance:
// file.php
require('../class/include.php');
// include.php
if(/* some condition */) {
$mydir = '...'; // should be '../class' in this example
header("Location: $mydir/setup.php");
die();
}
I have done the following with promising results, but it doesn't seem to always work and I am not sure if I am using the server variables wrong or if the configuration isn't correct for the instances that don't work.
$len = strlen($_SERVER['DOCUMENT_ROOT']);
$mydir = str_replace('\\', '/', __FILE__); // switch slashes for Windows systems
$mydir = substr($mydir, $len, strrpos($mydir, '/') - $len + 1);
NOTE I want the URL, the path doesn't help because I can't redirect to /var/wwwroot/some/directory
or C:\domains\mydomain\some\directory
This may be more a question of $_SERVER['DOCUMENT_ROOT']
. Notice I am stripping off the length of $_SERVER['DOCUMENT_ROOT']
from __FILE__
to get the web accessible portion.
EDIT
To make things a little simpler, is there a way to get the equivalent of $_SERVER['REQUEST_URI']
or $_SERVER['PHP_SELF']
for the included file rather than the parent file?