最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP: Browser go back a page - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Sep 19, 2011 at 3:54 dukevindukevin 23.2k37 gold badges87 silver badges113 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 7

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>

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

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

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.

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>";
      } 
   ?>
if($something) print '<script>window.history.go(-2);</script>';
发布评论

评论列表(0)

  1. 暂无评论