在 PHP 中重定向回上一页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23917152/
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
Redirect back to previous page in PHP
提问by RunCode
How do I redirect to a previous page using header("Location:...")
? The problem occurs when a user is scrolling down in a page to find a link for example, then clicks it - opens another page, clicks the link I've given "Go back to links (header("Location:links.php");
)", but when the user clicks it, it will head to previous page but on the top part of the page.
如何使用 重定向到上一页header("Location:...")
?例如,当用户在页面中向下滚动以查找链接,然后单击它时会出现问题 - 打开另一个页面,单击我提供的“返回链接 ( header("Location:links.php");
)”的链接,但是当用户单击它时,它将前往上一页,但在页面的顶部。
The user must scroll down again where he found the link he just clicked (which is frustrating). Is there a php code like the 'back'-buttonused in web browsers where you will go back to the exact location and page right before you click something else?
用户必须再次向下滚动找到他刚刚点击的链接(这令人沮丧)。是否有像Web 浏览器中使用的“后退”按钮这样的 php 代码,您可以在单击其他内容之前立即返回到确切的位置和页面?
回答by Ezhil
try this
尝试这个
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.
请注意,这可能不适用于安全页面 (HTTPS),而且总体而言这是一个非常糟糕的主意,因为标头可能会被劫持。
or
或者
header("location:javascript://history.go(-1)");
回答by Ali Gajani
Try this: header('Location: ' . $_SERVER['HTTP_REFERER']);
尝试这个: header('Location: ' . $_SERVER['HTTP_REFERER']);
'HTTP_REFERER'
'HTTP_REFERER'
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
将用户代理引用到当前页面的页面地址(如果有)。这是由用户代理设置的。并不是所有的用户代理都会设置这个,有些提供修改 HTTP_REFERER 的能力作为一个特性。简而言之,它不能真正被信任。
回答by Sergii Ivashchenko
Looks like you should add some javascript to the page to perform scrolling. Here is an example of such implementation with Jquery: how to remember scroll position of page
看起来您应该向页面添加一些 javascript 以执行滚动。下面是一个使用 Jquery 实现的例子:how to remember scroll position of page