php 重定向 10 秒倒计时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12498209/
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
Redirect 10 second Countdown
提问by user1662306
I have a page which redirects a user after 10 seconds with the following code.
我有一个页面,它在 10 秒后使用以下代码重定向用户。
<META HTTP-EQUIV="refresh" CONTENT="10;URL=login.php">
I then have this code which is echo'd in PHP, and would like the "10" (seconds) to countdown dynamically as 10, 9, 8, 7... so the user can see the seconds left until the page redirects.
然后我有这段在 PHP 中回显的代码,并希望“10”(秒)动态倒计时为 10、9、8、7 ……这样用户就可以看到页面重定向前剩余的秒数。
echo "We cant find you on the system. <br/> Please return to the <b><a href='login.php'>Login</a></b> page and ensure that <br/>you have entered your details correctly.
<br>
<br>
<b>Warning</b>: You willl be redirected back to the Login Page <br> in <b>10 Seconds</b>";
I was wondering if there was a way that this can be done in PHP, if not what would be the best way to achieve the same?
我想知道是否有一种方法可以在 PHP 中完成,如果没有,那么实现相同目标的最佳方法是什么?
回答by rationalboss
The following will redirect the user right away to login.php
以下将立即将用户重定向到 login.php
<?php
header('Location: login.php'); // redirects the user instantaneously.
exit;
?>
You can use the following to delay the redirection by X seconds but there is no graphical countdown (thanks to user1111929):
您可以使用以下命令将重定向延迟 X 秒,但没有图形倒计时(感谢user1111929):
<?php
header('refresh: 10; url=login.php'); // redirect the user after 10 seconds
#exit; // note that exit is not required, HTML can be displayed.
?>
If you want a graphical countdown, here's a sample code in JavaScript:
如果您想要图形倒计时,这里是 JavaScript 中的示例代码:
<p>You will be redirected in <span id="counter">10</span> second(s).</p>
<script type="text/javascript">
function countdown() {
var i = document.getElementById('counter');
if (parseInt(i.innerHTML)<=0) {
location.href = 'login.php';
}
if (parseInt(i.innerHTML)!=0) {
i.innerHTML = parseInt(i.innerHTML)-1;
}
}
setInterval(function(){ countdown(); },1000);
</script>
回答by TimTastic
I would use javascript for this
我会为此使用 javascript
var counter = 10;
setInterval(function() {
counter--;
if(counter < 0) {
window.location = 'login.php';
} else {
document.getElementById("count").innerHTML = counter;
}
}, 1000);?
Update: http://jsfiddle.net/6wxu3/1/
回答by Aleks G
You cannot do this with pure PHP - but javascript is your friend here.
你不能用纯 PHP 做到这一点 - 但 javascript 是你的朋友。
Change your HTML to put the number of seconds in a span:
更改您的 HTML 以将秒数放入 a span:
<b><span id="count">10</span> Seconds</b>
Then remove your metatag and use this javascript:
然后删除您的meta标签并使用此javascript:
var count = 10;
function decrement() {
count--;
if(count == 0) {
window.location = 'login.php';
}
else {
document.findElementById("count").innerHTML = "" + count;
setTimeout("decrement", 1000);
}
}
setTimeout("decrement", 1000);
This will decrement the count on the page every second and then redirect to login.phpwhen the counter reaches 0.
这将每秒减少页面上的计数,然后login.php在计数器达到 0 时重定向到。
回答by Marcus
header("Refresh: 2; url=$your_url");
header("Refresh: 2; url=$your_url");
Remember not to put any html content before the header.
请记住不要在标题之前放置任何 html 内容。

