返回上一页,带 header(“位置:”);在 PHP 中

发布于 2024-10-22 04:48:15 字数 105 浏览 6 评论 0原文

这个问题的标题有点解释了我的问题。如何使用 header( "Location: URL of previous page" ); 将 PHP 页面访问者重定向回其上一页

The title of this question kind of explains my question. How do I redirect the PHP page visitor back to their previous page with the header( "Location: URL of previous page" );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

心意如水 2024-10-29 04:48:15

尝试:

header('Location: ' . $_SERVER['HTTP_REFERER']);

请注意,这可能不适用于安全页面(HTTPS),并且总体而言这是一个非常糟糕的主意,因为标头可能被劫持,将用户发送到其他目的地。浏览器甚至可能不会发送标头。

理想情况下,您需要:

  • 将返回地址作为查询变量附加到请求(例如?back=/list)
  • 在代码中定义返回页面(即所有成功的表单提交都重定向到列表页面)
  • 提供用户可以选择下一步要去哪里(例如,保存并继续编辑或仅保存)

try:

header('Location: ' . $_SERVER['HTTP_REFERER']);

Note that this may not work with secure pages (HTTPS) and it's a pretty bad idea overall as the header can be hijacked, sending the user to some other destination. The header may not even be sent by the browser.

Ideally, you will want to either:

  • Append the return address to the request as a query variable (eg. ?back=/list)
  • Define a return page in your code (ie. all successful form submissions redirect to the listing page)
  • Provide the user the option of where they want to go next (eg. Save and continue editing or just Save)
玻璃人 2024-10-29 04:48:15

它是如此简单,只需使用它,

header("location:javascript://history.go(-1)");

它对我来说效果很好

Its so simple just use this

header("location:javascript://history.go(-1)");

Its working fine for me

迷爱 2024-10-29 04:48:15

只是一点补充:
我相信在标头函数之后添加 exit; 是一种常见且已知的事情,以防我们不希望加载或执行其余代码......

header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;

Just a little addition:
I believe it's a common and known thing to add exit; after the header function in case we don't want the rest of the code to load or execute...

header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
枫林﹌晚霞¤ 2024-10-29 04:48:15

您必须以某种方式保存该位置。

假设它是一个 POST 表单,只需将当前位置放在隐藏字段中,然后在 header() 位置中使用它即可。

You have to save that location somehow.

Say it's a POST form, just put the current location in a hidden field and then use it in the header() Location.

您的好友蓝忘机已上羡 2024-10-29 04:48:15

只需在 Javascript 中尝试一下:

 $previous = "javascript:history.go(-1)";

或者您可以在 PHP 中尝试一下:

if(isset($_SERVER['HTTP_REFERER'])) {
    $previous = $_SERVER['HTTP_REFERER'];
}

Just try this in Javascript:

 $previous = "javascript:history.go(-1)";

Or you can try it in PHP:

if(isset($_SERVER['HTTP_REFERER'])) {
    $previous = $_SERVER['HTTP_REFERER'];
}
何以笙箫默 2024-10-29 04:48:15

将以前的 url 存储在会话变量中是不好的,因为用户可能右键单击多个页面,然后返回并保存。

除非您将会话变量中的前一个 url 保存到表单中的隐藏字段并在 save header( "Location: save URL of Calling page" ); 之后

Storing previous url in a session variable is bad, because the user might right click on multiple pages and then come back and save.

unless you save the previous url in the session variable to a hidden field in the form and after save header( "Location: save URL of calling page" );

べ映画 2024-10-29 04:48:15

我知道这个问题特别关注标题,但如果有人想在按钮内执行此操作,这就足够了。

<button type="button" class="btn btn-default" onclick="javascript:history.go(-1)">Back</button>

I know this question focuses specifically on headers, but in case anyone would like to do it inside a button, this would suffice.

<button type="button" class="btn btn-default" onclick="javascript:history.go(-1)">Back</button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文