Php wait 5 seconds then redirect

Low-tech solution requiring no Javascript or even PHP:



    Redirecting...
    


    You are being automatically redirected to a new location.
If your browser does not redirect you in 10 seconds, or you do not wish to wait, click here.

The advantage of this solution over using the "Location:" header is that you don't need to pause the script execution, which will appear to the user as if the server is busy or their connection has hung.

This solution also gives the user the option of proceeding to the new page immediately rather than having to wait x number of seconds while their browser displays no information.

Edit: I think it's also worth noting that if you do decide to use the the header() method, you need to make sure your sleep() duration isn't too long. I think most browsers generate a connection timed out after not receiving any data from the server for 1 minute.

In this guide, we are going to show you how to redirect a web page after five seconds.

To carry out this “delayed redirect”, we can use PHP, JavaScript, or an HTML meta tag.

Note that you can easily modify these examples to suit your own needs. For example, if you want it to happen after 2 seconds, then you can simply replace 5 with 2.

Redirect using an HTML meta tag.

If you’re looking for a quick and easy way to redirect the page, then you can place the following meta tag inside the HEAD element of your HTML document:

A few points about this method:

  • The “http-equiv” tag defines what type of header we are setting. In this case, we gave it the value “refresh”.
  • Inside the content property, we declare that we want to redirect to the URL above after 5 seconds. If you want it to redirect after 2 seconds or 10 seconds, then simply replace the number 5.
  • Although the client can ignore HTTP headers like this, it will work in most browsers.
  • The content of the page WILL be displayed before the refresh takes place.

You can also create a “delayed redirect” using PHP.

If you are OK with the user seeing the content of the page, then you can use the following example:

//Set Refresh header using PHP.
header( "refresh:5;url=https://thisinterestsme.com/code" );

//Print out some content for example purposes.
echo 'This is the content.';

If you run the PHP snippet above, you’ll see that the message is displayed for five seconds.

After that, the redirect occurs.

This is basically the same as the first example. The only difference is that we are setting the header using PHP instead of using an HTML tag.

Redirecting with JavaScript.

You can also use JavaScript to redirect the user after a certain period of time:

In the JavaScript code above, we are using the setTimeout method to execute a function after a set period of time.

Inside the function, we replace the window.location.href property with the URL that we want the user to go to.

In this case, our function simply redirects the user to a different page. Please note that setTimeout’s second parameter must be in milliseconds.

In our example, we set it to 5,000 milliseconds, which equates to 5 seconds. If you want to change that to 2 seconds, then you will need to use 2,000 milliseconds instead.

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from PHP, and window.location method in JavaScript.

  • Redirect Pages In PHP
    • Method 1: Header Refresh Method
    • Method 2: Header Location Method
    • Conclusion

Here we are going to focus on redirecting a web page using PHP. Generally, we need to redirect a page after user authentication and user registration. We also redirect users to some specific conditions in our PHP script.

There are two main methods to redirect the user. However, both methods work on the header() function in PHP.

Read Also: Force HTTP to HTTPS Redirect

Method 1: Header Refresh Method

In this method, we need to pass refresh time and link to the web page where we want to redirect users.

Syntax

header( "refresh:0; url=YOUR_REDIRECT_LINK" );

Example 1

Note: Here refresh: 0; shows that page will wait for 0 seconds and redirect after that, which means the page will be instantly redirected. If you want users to wait for a few seconds and redirect after that,  you will simply need to change the refresh value.

Read Also: Registration and Login form with PHP

Example 2

In this example, users will wait at the current page for 5 seconds and then get redirected to another page.

Method 2: Header Location Method

This method is often used in PHP scripts to redirect pages instantly.

Syntax

header( "location: YOUR_REDIRECT_LINK" );

Example 1

Note: This method will not wait for a second and instantly redirect the page.

Example 2

We can use the sleep() method of PHP along with the above method to delay redirect for a few seconds. If we need to wait, users for 5 or 10 seconds we just need to use sleep(5) or sleep(10) before the header method.

Conclusion

This article has given a complete overview of PHP redirection and its methods.

We hope you have gained a complete understanding of the OOPs concepts in PHP programming. In case you want to learn about the other concepts, refer to our PHP Tutorial, PHP MySQL, PHP Form and PHP OOP sections. Happy Learning!

How to redirect after 5 seconds in PHP?

To redirect a webpage after 5 seconds, use the setInterval() method to set the time interval. Add the webpage in window. location. href object.

How to delay a redirect in PHP?

We can use the sleep() method of PHP along with the above method to delay redirect for a few seconds. If we need to wait, users for 5 or 10 seconds we just need to use sleep(5) or sleep(10) before the header method.

How do I redirect a page after a few seconds?

To redirect URL to a different website after few seconds, use the META tag, with the content attribute.

How do I redirect after delay?

If you want to redirect a page automatically after a delay then you can use the setTimeout() method in JavaScript and it will call the above property after a time delay. The solution is very simple. The setTimeout() method will call a function or execute a piece of code after a time delay (in millisecond).