0

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?

steveo225
  • 11,394
  • 16
  • 62
  • 114

4 Answers4

2

You can use dirname

$mydir = dirname(__FILE__);
hsz
  • 148,279
  • 62
  • 259
  • 315
  • All that really does is remove the 'include.php' from `__FILE__`. I need the directory to redirect to. That is why (in my example above), I am removing the length of `$_SERVER['DOCUMENT_ROOT']` from the beginning of `__FILE__`, but I have come across a couple PHP configurations where that doesn't work. – steveo225 Nov 07 '11 at 13:53
0

Your redirect should NOT be to a file as such, but to a URL.

If you use relative URLs its doesnt matter where the file is

eg: if "setup.php" is in the same directory as you are now

header("Location: setup.php");

and if it is in a subdir of the current location eg, setup/setup.php

header("Location: setup/setup.php");

Remember, this is going to send send the URL to the browser and the browser will then pull data from the new location, so just use relative URLs and then the whole problem of file paths goes away.

rszemeti
  • 91
  • 2
  • 1
    Yes, that is fine, but even creating the relative URLs is the problem as the file is included from an "unknown" source (meaning the file is included only and never accessed directly) and the directory may be many directories above or below the location I need to redirect to. – steveo225 Nov 07 '11 at 14:11
0

You need to use URLs to do the redirect and not file paths so try something like the following.

$base_url = 'http://example.org';
$path = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/'));
$file = 'example.php';
header("Location: {$base_url}/{$path}/{$file}";
exit;

$base_url could come from $_SERVER['SERVER_NAME'] for a more automated system.

Don't forget when issuing Location HTTP headers you should specify a full URL and not a relative one like you are in your question. See: RFC 1945 Hypertext Transfer Protocol -- HTTP/1.0: 10.11 Location.

Community
  • 1
  • 1
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • Except, `$_SERVER['REQUEST_URI']` is for the parent script, not the one included. Essentially I need the `REQUEST_URI` for the included script which may be many directories away from the parent script. – steveo225 Nov 07 '11 at 14:14
  • You need to employ a proper routing mechanism that will keep track of all this and allow you to generate routes. File paths do not directly translate to URLs. – Treffynnon Nov 07 '11 at 14:15
0

What I was doing does seem to work, the problem appears to be that $_SERVER['DOCUMENT_ROOT'] is unreliable and not necessarily correct. Found a better way to get the document root here (look at the second answer).

$docroot = realpath((getenv('DOCUMENT_ROOT') && ereg('^'.preg_quote(realpath(getenv('DOCUMENT_ROOT'))), realpath(__FILE__))) ? getenv('DOCUMENT_ROOT') : str_replace(dirname(@$_SERVER['PHP_SELF']), '', str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__))));
Community
  • 1
  • 1
steveo225
  • 11,394
  • 16
  • 62
  • 114