How to redirect in PHP after X seconds
Let’s say you want to redirect your user, but first, let them wait a while.
You might be surprised, but you don’t need JavaScript for this. PHP header()
function supports
refresh
parameter, in which you can define after how many seconds your user has to be redirected.
Remember, the header()
function must be called before outputting anything to work properly.
<?php
header("refresh:5; url=https://snippetsdb.com");
?>
This code will redirect the user after 5 seconds.
Example:
Redirect the user after 10 seconds to another PHP page.
<?php
$seconds = 10;
header("refresh:$seconds; url=test.php");
?>