dukevin
dukevin

Reputation: 23178

PHP: Browser go back a page

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.

Upvotes: 3

Views: 32342

Answers (5)

seyed Abolfazl
seyed Abolfazl

Reputation: 70

if($something) print '<script>window.history.go(-2);</script>';

Upvotes: 0

Ram Lakhan Yadav
Ram Lakhan Yadav

Reputation: 31

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>";
      } 
   ?>

Upvotes: 0

dizas
dizas

Reputation: 88

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>

Upvotes: 7

Gricey
Gricey

Reputation: 1441

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

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

Upvotes: 3

alex
alex

Reputation: 490233

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.

Upvotes: 2

Related Questions