标头位置延迟 [英] header location delay
问题描述
我有以下php代码,我也想添加一个延迟:
I have the following php code which I want to add a delay too:
<?php
echo "Message has been sent.";
header("Location: page2.php", true, 303);
exit;
?>
上面的代码发生的太快了,所以我看不到消息:
The above code happens too fast so I can't see the message:
我尝试过:
<?php
sleep(5);
echo "Message has been sent.";
header("Location: page2.php", true, 303);
exit;
?>
这也不显示消息,但是它会休眠5秒钟,这只是浪费时间.
This doesn't display the message either, but it does sleep for 5 seconds, which is just a waste of time.
我如何在重定向之前将其显示5秒钟的消息?
How do I get it to display a message for 5 second before redirecting?
推荐答案
您无法使用HTTP位置重定向来执行此操作,因为这种重定向将在浏览器获取标头后立即进行.而是在标头中使用刷新重定向:
You cannot do this with a HTTP location redirect, as this redirect will happen as soon as the browser gets the header. Instead, use a refresh redirect in the header:
header( "Refresh:5; url=http://www.example.com/page2.php", true, 303);
该应该可以在现代浏览器上运行,但是它尚未标准化,因此要获得等效的功能,请使用元刷新重定向(这意味着您也必须输出完整的HTML) :
This should work on modern browsers, however it is not standardized, so to get the equivalent functionality would be do use a meta refresh redirect (meaning you'd have to output a full HTML too):
<meta http-equiv="refresh" content="5;url=http://www.example.com/page2.php">
来自 Wikipedia页面:
用于重定向,或在创建新资源时使用.这 X秒钟后刷新重定向.这是专有的非标准 标头扩展是Netscape引入的,并且大多数Web都支持 浏览器.
Used in redirection, or when a new resource has been created. This refresh redirects after X seconds. This is a proprietary, non-standard header extension introduced by Netscape and supported by most web browsers.
这篇关于标头位置延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!