简体   繁体   English

PHP标头重定向到具有时间间隔的多个URL

[英]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? 我可以使用header()重定向到多个URL,并且之间存在时间间隔吗? Suppose i have url1 and url2. 假设我有url1和url2。 Now, what i want is that header first redirects to url1. 现在,我想要的是该标头首先重定向到url1。 Then say,after 5 seconds, it redirects me to url2. 然后说,在5秒钟后,它将我重定向到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. 但这立即使我进入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: 第1页:

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

PAGE 2: 第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). 您可以在网址之前添加“刷新:X”,但它仅对最后发送的标头有效(将被重定向到的网址)。

Actually I don't see any way to accomplish this unless you control what is on url1 or use some tricks with frames. 实际上,除非您控制url1上的内容或对框架使用一些技巧,否则我看不到任何实现此目的的方法。

When you have been redirected to url1 you no longer posses the control to do another redirection. 重定向到url1您将不再拥有该控件进行其他重定向的url1

You need to specifically redirect from url1 to url2 on url1 . 您需要专门从url1重定向到url2上的url1

You can also try iframe if you dont have control over url1 如果您无法控制url1也可以尝试使用iframe

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. 正如subirkumarsao,Martin和Edmunds所指出的那样,iframe是必经之路。 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. 现在exp2.php有一个提交按钮(出现在框架的底部),将我带到next.php。 And next.php has the code: next.php具有以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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