php 标头位置延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11299006/
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
header location delay
提问by oshirowanen
I have the following php code which I want to add a delay too:
我有以下 php 代码,我也想添加延迟:
<?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:
上面的代码发生得太快了,所以我看不到消息:
I have tried:
我试过了:
<?php
sleep(5);
echo "Message has been sent.";
header("Location: page2.php", true, 303);
exit;
?>
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?
如何让它在重定向前显示一条消息 5 秒?
回答by nickb
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:
您不能使用 HTTP 位置重定向来执行此操作,因为该重定向会在浏览器获取标头后立即发生。相反,在标头中使用刷新重定向:
header( "Refresh:5; url=http://www.example.com/page2.php", true, 303);
This shouldwork 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):
这应该适用于现代浏览器,但它不是标准化的,因此要获得等效的功能,请使用元刷新重定向(这意味着您也必须输出完整的 HTML):
<meta http-equiv="refresh" content="5;url=http://www.example.com/page2.php">
From the Wikipedia page:
从维基百科页面:
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.
用于重定向,或在创建新资源时使用。此刷新在 X 秒后重定向。这是由 Netscape 引入并被大多数 Web 浏览器支持的专有、非标准标头扩展。
回答by Emil Vikstr?m
Do the redirect using client-side scripting:
使用客户端脚本进行重定向:
<script>
window.setTimeout(function() {
window.location = 'page2.php';
}, 5000);
</script>
<p>Message has been sent.</p>
回答by Ali
You have to modify your code like this:
你必须像这样修改你的代码:
<?php
echo "Message has been sent.";
sleep(5);
header("Location: page2.php", true, 303);
exit;
?>
回答by Lusitanian
You won't be able to see the message if you're using a header(Location) redirect. In fact, that redirect shouldn't work at all since output starts before the headers are sent. Instead, you should echo a meta tag refresh with a delay, like this
echo '<meta http-equiv="refresh" content="5;URL=\'http://example.com/\'">';
which will have a delay of five seconds. Alternatively, (and more properly) you could output a JS redirect, as the meta refresh tag is deprecated.
如果您使用标头(位置)重定向,您将无法看到该消息。事实上,该重定向根本不应该工作,因为输出在发送标头之前就开始了。相反,您应该延迟回显元标记刷新,像这样
echo '<meta http-equiv="refresh" content="5;URL=\'http://example.com/\'">';
会有 5 秒的延迟。或者,(更恰当地说)您可以输出一个 JS 重定向,因为元刷新标签已被弃用。
回答by GabouhSk8
i don't know if it helps but as i was dealing with the same issue in a form which need to be clean after the submission of the message, i found this answer that explains that you could change the value input to be empty.
我不知道它是否有帮助,但由于我正在处理提交消息后需要清理的表单中的相同问题,我发现这个答案解释了您可以将值输入更改为空。
This is the answer: How do I reset the value of a text input when the page reloads?
这就是答案: 如何在页面重新加载时重置文本输入的值?
and this is how i fix my issue, using my class .form-control
这就是我如何解决我的问题,使用我的类 .form-control
$(document).ready(function() {
$(".form-control").val('');
});
回答by max son
header( "Refresh:time; url='filelocation'");
回答by Imagine R Power
This is what worked for me... I'm using the latest PHP version 7.3.3 on WAMPP for Windows...
这对我有用...我在 WAMPP for Windows 上使用最新的 PHP 版本 7.3.3...
<?php
$name = $_POST['name'] ?? null;
if(empty(trim($name))){
header("location: http://localhost/testsite/index.php");
}
echo 'Greetings '. $_POST['name'].', how can I help you?';
echo '<meta http-equiv="refresh" content="5;URL=\'http://localhost/testsite/index.php\'">';
I hope this helps.
我希望这有帮助。

