返回上一页,带 header(“位置:”);在 PHP 中
这个问题的标题有点解释了我的问题。如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试:
请注意,这可能不适用于安全页面(HTTPS),并且总体而言这是一个非常糟糕的主意,因为标头可能被劫持,将用户发送到其他目的地。浏览器甚至可能不会发送标头。
理想情况下,您需要:
try:
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:
它是如此简单,只需使用它,
它对我来说效果很好
Its so simple just use this
Its working fine for me
只是一点补充:
我相信在标头函数之后添加
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...您必须以某种方式保存该位置。
假设它是一个 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.只需在 Javascript 中尝试一下:
或者您可以在 PHP 中尝试一下:
Just try this in Javascript:
Or you can try it in PHP:
将以前的 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" );
我知道这个问题特别关注标题,但如果有人想在按钮内执行此操作,这就足够了。
I know this question focuses specifically on headers, but in case anyone would like to do it inside a button, this would suffice.