Reputation: 29
I'm defining a variable, $location, on a page called index.php and then using it when I set the header in a script called loginManager.php. If I simply use $location PHP insists that it is empty. However, if I echo the variable anywhere on the page it will recognize the contents of the variable.
Here is the relevant code:
index.php:
$location = $_SERVER['REQUEST_URI'];
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/loginManager.php");
loginManager.php:
header("Location: http://www.example.com/?location=$location");
echo $location;//Adding this line allows PHP to read the contents of $location.
//If this line is commented out PHP treats it as if it were empty.
The code running on loginManager.php is not in a function or anything that would cause scoping problems with $location. Does anyone have any idea why PHP would behave in this way?
EDIT: I will expand on how I know PHP thinks $location is empty when I do not echo it somewhere on the page.
If I do something like:
If(empty($location))
echo "location is empty";
else
print "location is not empty";
PHP will print out "location is empty".
However, if I do something like this
echo $location;
If(empty($location))
echo "location is empty";
else
print "location is not empty";
PHP will print out "location is not empty". It does not seem to matter where I echo $location out.
EDIT #2: @jx12345 pointed out that it's not $location specifically that needs to be echoed, just that something needs to be echoed in order for $location to be read.
Upvotes: 2
Views: 158
Reputation: 1670
Haven't you got some sort of weird loop happening here:
index.php loads "/lib/loginManager.php"
which redirects back to index.php with:
header("Location: http://www.example.com/?location=$location");
the echo just breaks the redirect
Try redirecting somewhere else, say test.php
header("Location: http://www.test.co.uk/test.php?location=$location");
and in there have something like:
print_r($_REQUEST);
see what happens
Upvotes: 1
Reputation: 19888
I think your problem might be a redirection loop. I'm betting that $_POST['caller']
is empty and you are redirecting multiple times to /
which then sets REQUEST_URI to blank.
Upvotes: 1
Reputation: 51
Try adding
global $location;
near the top of loginManager.php, if there's a variable scope issue that should take care of it.
Upvotes: 0