2

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.

29
  • 1
    what happens if you use header("Location: $_POST[caller]?location={$_SERVER['REQUEST_URI']}"); Commented Aug 1, 2013 at 19:45
  • 1
    What does a var_dump() say just to make sure? Commented Aug 1, 2013 at 19:47
  • var_dump($location) gives: string(16) "/tutoring/tutor/" which is exactly what $location should contain. Commented Aug 1, 2013 at 19:51
  • @Tavarius So why not try something like $location = "http://" . ($_SERVER['SERVER_NAME']) . ($_SERVER['REQUEST_URI']); Commented Aug 1, 2013 at 19:52
  • Also, $location has to be set on the other page because the whole point of it is to keep track of the page that is calling the loginManager.php script, so the other suggestions won't work. Commented Aug 1, 2013 at 19:53

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That isn't it either. I specifically make sure $_POST['caller'] is set right before that stuff in the code. I can also verify that this particular header is the one that is being used.
@OmarJackman The OP might have to add an if condition if caller is empty.
1

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

4 Comments

The key with the echo is that it happens after the header, which allows the header to still go through.
hmn true, isn't it looping though, and that's why its empty because header("Location: example.com/?location=$location"); doesn't redirect to header("Location: example.com/index.php?location=$location");
Sorry for the confusion. The original index.php is www.example.com/test/directory/index.php, and loginManager.php redirects to www.example.com/?location=/test/directory/index.php. So no, it's not looping back to itself.
are you able to try it on a different server?
0

Try adding

global $location;

near the top of loginManager.php, if there's a variable scope issue that should take care of it.

1 Comment

I have tried that and it doesn't work. I really don't think it's a scope issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.