简体   繁体   中英

PHP Header redirect to Multiple URLs with a time gap

Can i use header() to redirect to multiple URLs with a time gap in between? Suppose i have url1 and url2. Now, what i want is that header first redirects to url1. Then say,after 5 seconds, it redirects me to url2. Is there a way i can do that? I tried the following simple code

<?php
header("Location: url1");
header("Location: url2");
?>

But this takes me immediately to url2. I want a time gap in between. How can that be done? Thanks in advance.

That is not how it works. The redirect will take you to a new page...and THAT page would then have to do the next redirect. You should do something like:

PAGE 1:

<?php
header("Location: url1");
exit();
?>

PAGE 2:

<head>
<meta http-equiv="refresh" content="5; URL=url2">
</head>

No, you can't do that. Both headers are immediately sent to the browser, with the latter one overwriting the first one. You can add "Refresh: X" before the url, but it will only work for the last header sent (the URL that will get redirected to).

Actually I don't see any way to accomplish this unless you control what is on url1 or use some tricks with frames.

When you have been redirected to url1 you no longer posses the control to do another redirection.

You need to specifically redirect from url1 to url2 on url1 .

You can also try iframe if you dont have control over url1

In the header:

<script language="JavaScript">
var time = null
function move() {
myiframe.location.href = 'url2';
}
</script>

and this in the body:

<body onload="timer=setTimeout('move()',2000)"> //where 2000 is 2 seconds
<iframe id="myiframe" name="myiframe" src="url1"> </iframe>

Cheers

As pointed out by subirkumarsao, Martin and Edmunds, iframes is the way to go. Here is the final code:

<html>
<iframe src="url1" width="500" height="500"> </iframe>
</html>

<?php
include('exp2.php');
?>

Now exp2.php has a submit button(This appears at the bottom of frame) which takes me to next.php. And next.php has the code:

<?php
header("Location:url2");
exit();
?>

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