4

Is it possible to timely redirect back a user to the page from where he entered the page ?

if he entered the page from 1.php to ----> 2.php i need a delay redirect to 1.php

redirect always to the page where he entered the other page

OZ_
  • 12,492
  • 7
  • 50
  • 68
Sudantha
  • 15,684
  • 43
  • 105
  • 161

4 Answers4

11

You could use a meta tag:

<?php if (!empty($_SERVER['HTTP_REFERER'])) { ?>
    <meta http-equiv="refresh" content="2;url=<?php echo $_SERVER['HTTP_REFERER'] ?>">
<?php } ?>

Otherwise, you can use a JavaScript solution:

<head>
<script type="text/javascript">
setTimeout("window.location = '<?php echo $_SERVER['HTTP_REFERER'] ?>'", 2000);
</script>
</head>
webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • 3
    Good that you added `$_SERVER['HTTP_REFERER']`. – Midas May 23 '11 at 17:50
  • If you're using either of these solutions, you should add a text link on the page you are redirecting _from_, so that if meta/JavaScript don't work, at least your users have something to see/click on. – Kevin Ji May 24 '11 at 01:07
3

PHP isn't dynamic (and not clientside) so you can't make timers. You'll have to use a meta tag or JavaScript. See this answer: Redirect with PHP.

Community
  • 1
  • 1
Midas
  • 7,012
  • 5
  • 34
  • 52
1

If they got through by entering data in a form, then pass the page1 address back to page2 in a hidden field.

page1.php -> puts out a form

<form action = page2.php method=post>
<input type=hidden name=return value=page1 />
<input type = submit />
</form>

page2.php -> handles the form, detects where to return the user

$next = 'default-welcome-page';
$permitted = array('page1','page3','page4');
if( isset($_POST['return'])
&& in_array($_POST['return'], $permitted) ){
$next = $_POST['return'];
}
header(Location: $next.'php');
exit;

Otherwise you could construct a html link, and do the same as above but using the $_GET array instead of $_POST

<a href=page.php?return=page1>Go on, go on...</a>
Cups
  • 6,901
  • 3
  • 26
  • 30
0

Old thread, but perhaps useful nonetheless:

header ("Refresh:5; URL=proceed.php");

Just make sure to play by the rules, header must come first before anything.