use*_*306 14 html php redirect
我有一个页面,使用以下代码在10秒后重定向用户.
<META HTTP-EQUIV="refresh" CONTENT="10;URL=login.php">
Run Code Online (Sandbox Code Playgroud)
然后我将这个代码在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>";
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种方法可以在PHP中完成,如果不是最好的方法来实现相同的?
rat*_*oss 41
以下内容将立即将用户重定向到 login.php
<?php
header('Location: login.php'); // redirects the user instantaneously.
exit;
?>
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令将重定向延迟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.
?>
Run Code Online (Sandbox Code Playgroud)
如果您想要一个图形倒计时,这里是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>
Run Code Online (Sandbox Code Playgroud)
我会用javascript来做这件事
var counter = 10;
setInterval(function() {
counter--;
if(counter < 0) {
window.location = 'login.php';
} else {
document.getElementById("count").innerHTML = counter;
}
}, 1000);?
Run Code Online (Sandbox Code Playgroud)
更新:http: //jsfiddle.net/6wxu3/1/
你不能用纯PHP做到这一点 - 但javascript是你的朋友.
更改HTML以将秒数放入span:
<b><span id="count">10</span> Seconds</b>
Run Code Online (Sandbox Code Playgroud)
然后删除您的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);
Run Code Online (Sandbox Code Playgroud)
这将每秒递减页面上的计数,然后login.php在计数器达到0时重定向到.