Header() in PHP – Refresh (Redirect) to Location (URL) in X seconds

Recently I was designing a simple user system for one of my websites. Obviously, it had login and logout feature. Now when I designed the login page, on a successful login, I wanted the user to be redirected to index page without displaying any kind of message. Whereas when I designed the logout page, I wanted the page to display “Logged out successfully” message for 5 seconds, then redirect to index page.

Now you can achieve this in HTML using the meta tag which I will be showing at the end of this tutorial, but if you wish to do this in PHP, it’s a little bit tricky. Following are the two cases and their solutions.

Case 1 : Redirect a page to a URL without waiting in PHP.

Say, after a successful login, you wish to redirect the user to index.php without waiting. Following is the code that can be used for the same :

header("Location: index.php");

Make sure that no text is sent to the browser before this part of the script is executed. Since header() is a function which is used to set “Headers” for a page when it is opened in a browser. In simpler words, when the browser starts receiving data from the server, it takes the header messages first and then the data that is supposed to be displayed. Now, if you send any data to be displayed, obviously you cannot send header messages any longer. So always make it a point to use “header()” before sending any kind of data to be displayed.

Case 2 : Redirect a page to an URL after waiting for X seconds in PHP.

Let us take the same example in the above scenario but let’s say that this time you wish to show a message saying logged in successfully, then wait for 5 seconds and redirect to index page. Here’s the code to do it :


<?php

header("Refresh: 5; url=index.php");

echo 'Logged in successfully.'; ?>

Please note that I have used “echo” statement AFTER using “header()” function. I have briefly explained the logic behind this in Case 1. If you use an echo statement or display any text before setting headers in a PHP page, the header function won’t work. So make sure you first set the headers and then use echo statement.

Bonus : HTML code to redirect a webpage after X seconds.

Following is the code to redirect a webpage to an URL (for e.g. http://nimishprabhu.com) after, say, 5 seconds.

<meta http-equiv="refresh" content="5; url=http://nimishprabhu.com/">

Just place the above code in the head section of the page i.e. after <head> and before </head> tag. This will refresh (redirect) a page to a specific URL after specified number of seconds.

Hope this article was useful. If you have any doubts, feel free to drop a comment using the form below.

This entry was posted in PHP.

2 thoughts on “Header() in PHP – Refresh (Redirect) to Location (URL) in X seconds

  1. This is a great work around to having an intro video that will not redirect to another url. It’s not as smooth, but it works.

    If you have a working code for an “end of intro video” redirect, I’d love to see it. It seems everyone posts the same code but I have never been able to make it work. The site I used this on is http://www.elementzband.com

Leave a Reply

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