简体   繁体   English

用PHP重定向到上一页的最简单方法是什么?

[英]What's the easiest way to redirect to the previous page with PHP?

I'm using a form to submit some post information to a PHP script. 我正在使用一种表单向PHP脚本提交一些帖子信息。 After the script finishes, I want it to redirect right back to the page the user came from. 脚本完成后,我希望它立即重定向回用户所在的页面。 Right now I'm just using header() with a static URL. 现在,我只是使用带有静态URL的header()。 I've found a ton of very conflicting information about this around the internet, so I'm wondering what StackOverflow thinks. 我在互联网上发现了许多与此非常矛盾的信息,所以我想知道StackOverflow的想法。

使用HTTP_REFERER

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

access the $_SERVER['HTTP_REFERER'] variable and redirect to this. 访问$ _SERVER ['HTTP_REFERER']变量并重定向到此变量。 Should do the trick. 应该做到的。

the way i would do it is use a session variable to store the current page URL everytime it is accessed. 我要做的方式是使用会话变量来存储每次访问时的当前页面URL。

$_SESSION['last_url'] = <get current url>

replace your static url in the header with $_SESSION['last_url']. 用$ _SESSION ['last_url']替换标题中的静态网址。 Depending on how you implement your PHP, you can use search google for "current url php" or just $_SERVER['REQUEST_URI'] (stackoverflow doesnt allow me to put more than 1 link!) 根据您实现PHP的方式,您可以使用Google搜索搜索“当前URL php”,也可以仅使用$ _SERVER ['REQUEST_URI'](stackoverflow不允许我放置多个链接!)

使用REQUEST_URI,但要注意该斜杠/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM