为什么我的 PHP header('Location:...') 调用忽略 HTTP GET 请求?
我有 3 个文件:account.php、index.php 和 browser.php。
成功登录后,account.php 调用以下函数:
header('Location: index.php?m='.base64_encode('Signed in'));
index.php 调用:
header('Location: ./browser.php?m='.$_GET['m']);
browser.php 显示:
if(isset($_GET['m']))
echo '<h3 style="color:green">'.base64_decode($_GET['m']).'</h3>';
问题是header('Location:...') 从 index.php 到 browser.php 的调用会丢弃 '?' 之后的任何内容。每次 url 都只显示 browser.php
发生了什么?
[编辑]
好吧,问题已经解决了,通过index.php中一些可怕的if else逻辑。 执行以下操作不起作用(将始终运行最新的标头):
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');
我必须像这样重组它:
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');
但坦率地说,这都是无关紧要的,我应该使用 $_SESSION 来保留重定向上的变量。谢谢大家!
I have 3 files, account.php, index.php, and browser.php.
Upon 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1)为什么使用
base64_encode
,而不是urlencode
?2)
Location
标头应该是一个完整的url,包括协议和服务器名称(如果我没记错的话)。3)参见knittl的评论。
1) Why do you use
base64_encode
, noturlencode
?2)
Location
header should be a complete url, including the protocol and server name (if I recall correctly).3) See knittl's comment.
为什么结构这么复杂?为什么不直接从帐户重定向到浏览器?
尽管浏览器对 url 语法的容忍度极高,但它应该是完全限定的 url,从 http:// 开始。我怀疑
./
可能是原因,但为什么不把它只是 browser.php 呢?无论如何,您必须调试代码才能发现实际问题。
首先,您必须观察正在发送的实际 HTTP 请求。 Firebug 或 LiveHTTPHeaders Firefox 插件可以做到这一点。
打印出所有位置字符串是绝对必要的。只需依次用 echo() 替换所有 header() 函数,然后查看代码要发送的实际标头
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
您显示的代码是完美的并且应该可以工作。问题一定出在其他地方。您可以使用 Live HTTP Headers、Firebug 或类似工具检查标头,看看发生了什么?我认为你的问题在其他地方。
编辑:其他答案提到相对网址不符合标准。这是事实,但是虽然 RFC 禁止它们,但它们确实有效(尽管我不会写 ./browser.php,而只是写 browser.php)。
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).