How to redirect after 5 seconds in PHP?

Member

by lily , in category: PHP General , 2 years ago

How to redirect after 5 seconds in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by dana , a year ago

@lily 

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:

1
2
3
<?php
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:

1
<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.

by jasen_gottlieb , 8 months ago

@lily 

To redirect after 5 seconds in PHP, you can use the header() function along with the refresh meta tag in HTML. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13




  
  Redirecting...


  Redirecting...
  You'll be redirected to the destination page in 5 seconds.



In this example, the header() function sets the HTTP response header to refresh the page after 5 seconds and redirect to the specified destination.php page. The meta tag in the HTML will also contribute to the redirection.