简体   繁体   中英

PHP delay redirect back to a page

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

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>

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 .

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM