PHP: Browser go back a page

我的未来我决定 提交于 2019-12-13 17:43:15

问题


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?

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


回答1:


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>



回答2:


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

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



回答3:


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

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

As a side note, always exit after sending the Location HTTP header. User agents don't have to follow it.




回答4:


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

code:

  <?php

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


来源:https://stackoverflow.com/questions/7466286/php-browser-go-back-a-page

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