返回上一页与标题(“位置:”);在 PHP 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5285031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Back to previous page with header( "Location: " ); in PHP
提问by Web_Designer
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" );
这个问题的标题解释了我的问题。如何将 PHP 页面访问者重定向回他们的上一页header( "Location: URL of previous page" );
回答by Dimitry
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 hiHymaned, sending the user to some other destination. The header may not even be sent by the browser.
请注意,这可能不适用于安全页面 (HTTPS),而且总体而言这是一个非常糟糕的主意,因为标头可能会被劫持,将用户发送到其他目的地。浏览器甚至可能不会发送标头。
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)
- 将返回地址作为查询变量附加到请求中(例如?back=/list)
- 在您的代码中定义一个返回页面(即所有成功提交的表单都重定向到列表页面)
- 为用户提供下一步要去哪里的选项(例如,保存并继续编辑或仅保存)
回答by Hammad
Its so simple just use this
它是如此简单,只需使用这个
header("location:javascript://history.go(-1)");
Its working fine for me
它对我来说很好用
回答by Your Common Sense
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.
假设它是一个 POST 表单,只需将当前位置放在一个隐藏字段中,然后在header()
位置中使用它。
回答by 6opko
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...
只是一点点补充:我相信exit;
在标头函数之后添加是一种常见且众所周知的事情,以防我们不希望其余代码加载或执行...
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
回答by Minu Alex
Just try this in Javascript:
只需在 Javascript 中试试这个:
$previous = "javascript:history.go(-1)";
Or you can try it in PHP:
或者你可以在 PHP 中尝试:
if(isset($_SERVER['HTTP_REFERER'])) {
$previous = $_SERVER['HTTP_REFERER'];
}
回答by rudy
Storing previous url in a session variable is bad, because the user might right click on multiple pages and then come back and save.
将先前的 url 存储在会话变量中是不好的,因为用户可能会右键单击多个页面,然后返回并保存。
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" );
除非您将会话变量中的前一个 url 保存到表单中的隐藏字段并在保存 header("位置:调用页面的保存 URL") 之后;