PHP 重定向暂停
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/736989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Redirect Pause
提问by Paradius
How do I pause a page for a certain amount of seconds before redirecting the user to another HTML/PHP page using PHP?
如何在使用 PHP 将用户重定向到另一个 HTML/PHP 页面之前暂停页面一定秒数?
回答by Strae
This one should works:
这个应该有效:
<?php
header('Refresh: 5; URL=http://yoursite.com/page.php');
//other code
?>
and will allow your user to see whatever kind of output you want (You'll be redirected in X Seconds, click yere if dont, etc..)
并允许您的用户看到您想要的任何类型的输出(您将在 X 秒内被重定向,如果没有,请单击 yere,等等..)
回答by Calvin
Low-tech solution requiring no Javascript or even PHP:
不需要 Javascript 甚至 PHP 的低技术解决方案:
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh"
content="10;URL=http://www.newsite.com">
</head>
<body>
You are being automatically redirected to a new location.<br />
If your browser does not redirect you in 10 seconds, or you do
not wish to wait, <a href="http://www.newsite.com">click here</a>.
</body>
</html>
The advantage of this solution over using the "Location:" header is that you don't need to pause the script execution, which will appear to the user as if the server is busy or their connection has hung.
与使用“Location:”标头相比,此解决方案的优势在于您不需要暂停脚本执行,这在用户看来就好像服务器正忙或他们的连接已挂起一样。
This solution also gives the user the option of proceeding to the new page immediately rather than having to wait x number of secondswhile their browser displays no information.
此解决方案还为用户提供了立即进入新页面的选项,而不必在浏览器不显示任何信息时等待x 秒。
Edit: I think it's also worth noting that if you do decide to use the the header() method, you need to make sure your sleep() duration isn't too long. I think most browsers generate a connection timed outafter not receiving any data from the server for 1 minute.
编辑:我认为还值得注意的是,如果您决定使用 header() 方法,则需要确保您的 sleep() 持续时间不会太长。我认为大多数浏览器在 1 分钟内未从服务器接收任何数据后都会生成连接超时。
回答by Csaba Kétszeri
neveruse sleep this way. Even under slight load your server will run out of http connections. Worst if your firewall runs out.
永远不要以这种方式使用睡眠。即使在轻微负载下,您的服务器也会耗尽 http 连接。最糟糕的是,如果您的防火墙用完了。
The delay parameter in the redirect header is made for exactly to the same reason.
重定向标头中的延迟参数是出于完全相同的原因。
回答by Inktown
If you want to wait and then go back to the previous page here's how
如果您想等待然后返回上一页,请按以下步骤操作
$referrer = $_SERVER['HTTP_REFERER'];
header ("Refresh: 2;URL='$referrer'");
回答by Daan
Update: this works, but may not be the most appropriate solution for this situation. See comments.
更新:这有效,但可能不是这种情况的最合适的解决方案。看评论。
Might this be what you are looking for?
这可能是您要找的吗?
<?php
sleep(5);
header("Location: http://www.example.com/");
exit();
?>
回答by Darryl Hein
The other 2 options are a Javascript redirect using setTimeout() or a meta refresh tag with a timeout.
其他 2 个选项是使用 setTimeout() 的 Javascript 重定向或带有超时的元刷新标记。
回答by user3192948
Try:
尝试:
<?php
$url=base_url().'page_you_want_to_go_to'
header('Refresh: X; URL=$url');
?>
This will give you additional flexibility when exchanging between localhost and remote server.
在本地主机和远程服务器之间进行交换时,这将为您提供额外的灵活性。
回答by Bramos
put this in the header.
把它放在标题中。
<meta http-equiv="refresh" content="0.01;makensucces.html" />
on the place 0.01 you must put the seconds, i have used an decimal number.
在地方 0.01 你必须把秒,我用了一个十进制数。
and on the place of makensucces.html you need to put the name off the file you desire.
在 makensucces.html 的位置,您需要将名称从您想要的文件中删除。

