PHP Redirect After 5 Seconds with Examples

Last Updated on October 16, 2022 by Roshan Parihar

In this tutorial, learn how to redirect after 5 seconds in PHP. The short answer is to use the header() function and pass time in seconds with the URL of the page.

You can also use the setTimeout() function of javascript in PHP for redirection. Let’s find out the different examples to redirect pages in PHP.

Method 1: Redirect After 5 Seconds Using header() Function in PHP

To redirect after 5 seconds in PHP, you have to use the header() and pass URL as its parameter. Also, pass redirect:5; before the URL in the function. See the example given below to learn the method of redirection in PHP.

You have to just add the above code to your website PHP code. Also, change the URL with your required URL. During the time of PHP code execution, when it reaches the above code, it will redirect to the specified URL after 5 seconds.

Method 2: With setTimeout() Function of Javascript in PHP

If you want to make redirection in PHP, you can also add the javascript code with your PHP code. You have to just add the setTimeout() function with its first argument as a URL. The second argument is the time in milliseconds. For 5 seconds, you have to pass 5000 to redirect after 5 seconds.

Add the above code to your PHP codes and change the URL for redirection. It’s a simple script to redirect one page to another page after 5 seconds.

Method 3: Using <meta> Tag of HTML in PHP Pages

In addition to the above method, you can also use the meta tag as given in the example below. You just have to change the URL with your required URL.

However, the meta tag is not useful when you want to use conditions in PHP codes for redirection.

The first and second examples are useful examples you can use for redirection.

You May Also Like to Read

Reference

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.