Javascript PHP:浏览器返回一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7466286/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:24:04  来源:igfitidea点击:

PHP: Browser go back a page

phpjavascriptback

提问by lemon

I want for something like this:

我想要这样的东西:

<?php
if($something)
    header('Location:javascript:history.go(-2)');
?>

but header('Location:javascript:history.go(-2)');doesn't work. Any alternatives?

header('Location:javascript:history.go(-2)');不起作用。任何替代方案?

I don't know the page the user was just on, so I can't hardcode the url.

我不知道用户刚刚所在的页面,所以我无法对 url 进行硬编码。

回答by dizas

From your point of view, i think you might be looking for something like this:

从你的角度来看,我认为你可能正在寻找这样的东西:

<html><head></head><body>
<?php
if($something){
?>
<script type="text/javascript">
window.history.go(-2);
</script>
<?php
}
?>
</body></html>

回答by Gricey

As long as they got there by a link you could use

只要他们通过链接到达那里,您就可以使用

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

回答by alex

You can't use the javascript:pseudo protocol in a LocationHTTP header. In fact, you really shouldn't be using it at all.

您不能javascript:LocationHTTP 标头中使用伪协议。事实上,你根本不应该使用它。

Instead, send the whole URL to the page you want the user to go back to via the LocationHTTP header (if you can) oryou could echo the history.back()though I highly discourage that you do.

相反,将整个 URL 发送到您希望用户通过LocationHTTP 标头返回的页面(如果可以), 或者您可以回显history.back()尽管我非常不鼓励您这样做

As a side note, always exitafter sending the LocationHTTP header. User agents don't have to follow it.

作为旁注,总是exit在发送LocationHTTP 标头之后。用户代理不必遵循它。

回答by Ram Lakhan Yadav

According to your problem .you can use following code in php script at any where..

根据您的问题。您可以在 php 脚本中的任何地方使用以下代码..

code:

代码:

  <?php

     if($something){
       echo "<script>window.history.back()</script>";
      } 
   ?>