How to do this:
"you will be redirected to domain.com in 5..(4,3,2,1) seconds"
in php???
How to do this:
"you will be redirected to domain.com in 5..(4,3,2,1) seconds"
in php???
This is best not done in PHP, because it would require the web server to keep the connection to the client open while the count down runs.
This would (IMHO) be far better done in Javascript with setInterval()
.
Short answer: You can't.
Long answer: PHP is serverside and as such as soon as the page is sent PHP has nothing to do with it. What you're looking for is possible in a number of ways. You can either use javascript (which allows you to do the 'visual' countdown) or set the meta refresh tag in the element of your document, as such: <meta http-equiv="refresh" content="5;url=http://yoursite.com">
I'm pretty sure javascript is your best option. The only other way to do it would be to redirect to a new URL every second, which is overkill in my opinion.
Here's full source code for a sample countdown:
<html>
<head>
<meta http-equiv="refresh" content="5;url=http://example.com" />
<title>Countdown Sample</title>
</head>
<body>
you will be redirected to example.com in <span id="seconds">5</span>.
<script>
var seconds = 5;
setInterval(
function(){
document.getElementById('seconds').innerHTML = --seconds;
}, 1000
);
</script>
</body>
</html>
Here's an improved version with Alnitak's advice:
I've changed the JavaScript to redirect users and prevent the countdown from going below 1 and I've added a <noscript>
tag around the <meta>
for users without JavaScript.
<html>
<head>
<noscript>
<meta http-equiv="refresh" content="5;url=http://example.com" />
</noscript>
<title>Countdown Sample</title>
</head>
<body>
you will be redirected to example.com in <span id="seconds">5</span>.
<script>
var seconds = 5;
setInterval(
function(){
if (seconds <= 1) {
window.location = 'http://example.com';
}
else {
document.getElementById('seconds').innerHTML = --seconds;
}
},
1000
);
</script>
</body>
</html>
You should use javascript to visualize a countdown, in php you can set a timer for the redirection like this:
header('refresh: 5; url=http://www.stackoverflow.com');
where refresh: x set the time in seconds to wait for the redirection.
You can implement a Javascript countdown with the setInterval()
function, called every second for 5 seconds to create a timer function to count down the time. When the time has elapsed, then you can use the document
's location.href
property to redirect the user to the page; it would also be beneficial to create a hard link to direct them to the page, in case Javascript is turned off, for instance.
For example:
var timeLeft, id;
function startCountDown()
{
timeLeft = 5;
id = setInterval(countDown, 1000);
}
function countDown()
{
timeLeft -= 1;
//Update the text which states how much time is remaining until the
//user is redirected here..
if(timeLeft==0)
{
clearInterval(id);
//Redirect to the desired URL
document.location.href = "http://domain.com";
}
}
You can mix javascript and php, when you enter page you get a date you need to countdown to from database through php and then count it down with javascript. Something like that is used by http://free-countdown.co.nr/