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.
var_dump()say just to make sure?$location = "http://" . ($_SERVER['SERVER_NAME']) . ($_SERVER['REQUEST_URI']);