Why does my PHP header('Location:...') call disregard the HTTP GET request?
I have 3 files, account.php, index.php, and browser.php.
Upo开发者_如何学Cn a successful login account.php calls the following function:
header('Location: index.php?m='.base64_encode('Signed in'));
To which index.php calls:
header('Location: ./browser.php?m='.$_GET['m']);
And browser.php displays:
if(isset($_GET['m']))
echo '<h3 style="color:green">'.base64_decode($_GET['m']).'</h3>';
The problem is the header('Location:...') call from index.php to browser.php drops anything after the '?'. Everytime the url just displays browser.php
What is going on?
[EDIT]
Well the problem was fixed, by some horrible if else logic in the index.php. Doing the following does not work (will always run the latest header):
if(isset($_GET['m']))
header('Location: browser.php?m='.$_GET['m']);
if(isset($_GET['e']))
header('Location: browser.php?e='.$_GET['e']);
header('Location: browser.php');
I had to restructure it like so:
if(isset($_GET['m']))
header('Location: browser.php?m='.$_GET['m']);
else if(isset($_GET['e']))
header('Location: browser.php?e='.$_GET['e']);
else
header('Location: browser.php');
But frankly this is all irrelevant, I should be using $_SESSION to preserve variables on redirects. Thanks everyone!
1) Why do you use base64_encode
, not urlencode
?
2) Location
header should be a complete url, including the protocol and server name (if I recall correctly).
3) See knittl's comment.
Why so complex structure? Why not to redirect from account directly to the browser?
Despite of the fact that browsers being extremely tolerant to the url syntax, it should be fully qualified url, beginning from http://. I doubt that ./
can be cause but why don't you make it just browser.php?
Anyway, you have to debug your code to spot the actual problem.
First of all you have to watch actual HTTP requests being sent. Firebug or LiveHTTPHeaders Firefox addons can do it.
To print out all location strings is absolutely necessary. Just substitute in turns all header() functions with echo() and see what actual header your code is about to send
The code you are showing is faultless and should work. The problem must be elsewhere. Can you check the headers using Live HTTP Headers, Firebug or similiar and see what is happening? I think your problem is elsewhere.
edit: Other answers mention that relative urls are not standards-complient. This is true, but while RFCs forbid them, they do work (although I would not write ./browser.php but simply browser.php).
精彩评论