Redirect 10 second Countdown

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>

I would use javascript for this

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/

Tags:

Html

Php

Redirect