Redirect with Timer in PHP?

You can cause your PHP script to sleep for 10 seconds,

sleep(10);

but this will appear to the end-user as a non-responsive server. The best option is to use either a meta refresh,

<meta http-equiv="refresh" content="10;url=http://google.com">

or javascript.

setTimeout(function(){
  window.location = "http://google.com";
}, 10000);

Found in the comments from Daniel:

header('Refresh: 10; URL=http://yoursite.com/page.php');

would be ideal for this situation, as it requires no Javascript or HTML.


header('Refresh: 10; URL=http://yoursite.com/page.php');

Place this PHP code inside header section of the page, otherwise, it wouldn't work.


You'll want to do a client side redirect:

<meta http-equiv="refresh" content="5;url=http://yourdomain.com"/>

but if you feel like it has to be in PHP, you could do something like:

<?php

// wait 5 seconds and redirect :)
echo "<meta http-equiv=\"refresh\" content=\"5;url=http://yourdomain.com\"/>";

?>

Tags:

Php