PHP 重定向而不修改标头

发布于 2024-07-16 02:42:24 字数 334 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分分钟 2024-07-23 02:42:24

您可以尝试的一件事是:不要发送 Location 标头,而是发回带有

<meta http-equiv="refresh" content="0;url=http://someurl/somepage.php">

部分中的标记的 HTML 页面。 我不确定它是否能解决您的问题,但...这实际上取决于浏览器的行为方式。 $_SERVER['HTTP_REFERER'] 的值来自 Referer 请求标头,浏览器可以自行决定是否发送该标头。

您还可以做的其他事情是

header("Location: http://someurl/somepage.php?referer=http%3A%2F%2Fthisurl%2Fthispage.php");

将引用 URL 作为查询字符串参数发送,然后您可以通过 $_GET['referer'] 访问它。 这可能比依赖浏览器发送引用标头更可靠。

One thing you could try: instead of sending the Location header, send back an HTML page with the tag

<meta http-equiv="refresh" content="0;url=http://someurl/somepage.php">

in 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 the Referer request header which the browser is free to send or not send, at its discretion.

Something else you could do is

header("Location: http://someurl/somepage.php?referer=http%3A%2F%2Fthisurl%2Fthispage.php");

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.

明明#如月 2024-07-23 02:42:24

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文