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

 ̄綄美尐妖づ 提交于 2020-01-05 15:10:13

问题


I'm using a form to submit some post information to a PHP script. 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. I've found a ton of very conflicting information about this around the internet, so I'm wondering what StackOverflow thinks.


回答1:


Use HTTP_REFERER:

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



回答2:


access the $_SERVER['HTTP_REFERER'] variable and redirect to this. Should do the trick.




回答3:


the way i would do it is use a session variable to store the current page URL everytime it is accessed.

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

replace your static url in the header with $_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!)




回答4:


Use REQUEST_URI But watch out for that presiding slash/



来源:https://stackoverflow.com/questions/4282008/whats-the-easiest-way-to-redirect-to-the-previous-page-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!