How to redirect after 5 seconds in PHP?


by lily , in category: PHP General , a year ago

How to redirect after 5 seconds in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer


by dana , 5 months ago


You can use the header() function in PHP to redirect the browser to a new page. To delay the redirect, you can use the sleep() function to pause the script for a number of seconds before calling header().

Here's an example of how you can delay a redirect by 5 seconds:

sleep(5); // Delay for 5 seconds
header('Location: /new-page.php'); // Redirect to new page

Note that the sleep() function will cause the script to pause for the specified number of seconds before continuing to execute. This means that the browser will not display any content from the script until the sleep period has finished.

If you want to display a message to the user before the redirect occurs, you can use an HTML meta refresh tag instead. This will allow the message to be displayed while the redirect is being delayed:

<meta http-equiv="refresh" content="5; URL='/new-page.php'">

This will display the current page for 5 seconds before redirecting the browser to the new page.