Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

php - Redirect 10 second Countdown

I have a page which redirects a user after 10 seconds with the following code.

<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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following will redirect the user right away to 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):

<?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:

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...