PHP 重定向而不修改标头
我有一个 PHP 脚本,它通过一些代码重定向用户,例如:
header ('Location: http://someurl/somepage.php');
然后在 somepage.php 中我尝试访问 < code>$_SERVER['HTTP_REFERER'] 来确定页面请求来自何处。 我发现使用标头位置方法调用页面时 $_SERVER['HTTP_REFERER']
为空。
是否有其他方法可以用来重定向用户,以便我仍然可以在目标页面中使用 $_SERVER['HTTP_REFERER']
。
I have a PHP script that is redirecting the user via some code like:
header ('Location: http://someurl/somepage.php');
Then in somepage.php I am trying to access $_SERVER['HTTP_REFERER']
to determine where the page request has come from. I find that $_SERVER['HTTP_REFERER']
is empty when a page is called using the header location method.
Is there an alternative method that I can use to redirect the user so that I can still use $_SERVER['HTTP_REFERER']
in the target page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试的一件事是:不要发送
Location
标头,而是发回带有部分中的标记的 HTML 页面。 我不确定它是否能解决您的问题,但...这实际上取决于浏览器的行为方式。
$_SERVER['HTTP_REFERER']
的值来自Referer
请求标头,浏览器可以自行决定是否发送该标头。您还可以做的其他事情是
将引用 URL 作为查询字符串参数发送,然后您可以通过
$_GET['referer']
访问它。 这可能比依赖浏览器发送引用标头更可靠。One thing you could try: instead of sending the
Location
header, send back an HTML page with the tagin the
<head>
section. I'm not sure if it would fix your problem, though... it really depends on how the browser behaves. The value of$_SERVER['HTTP_REFERER']
comes from theReferer
request header which the browser is free to send or not send, at its discretion.Something else you could do is
that is, send the referer URL as a query string parameter, then you can access it as
$_GET['referer']
. This is probably more reliable than relying on the browser to send a referer header.HTTP Referer: 标头由用户代理设置,而不是由服务器设置。 我不确定您可以采取什么措施来确保获得 Referer 标头。
The HTTP Referer: header is set by the user agent, and not by the server. I'm not sure that there is anything you can do to ensure that you get a Referer header.