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.
1 2 3 |
<?php header( "refresh:5;URL=https://tutorialdeep.com/" ); ?> |
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.
1 2 3 4 5 |
<script> setTimeout(function(){ window.location.href = 'https://tutorialdeep.com/php'; },5000);// for 5 second redirection </script> |
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.
1 |
<meta http-equiv="refresh" content="5;url=https://tutorialdeep.com/sql"> |
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
- PHP Redirect to Another Page with Examples
- MySQL Cannot Connect Invalid Settings in XAMP (Resolved)
- How to Make Contact Form and Send Email in PHP
Reference