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 PHP header() function 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");
 //notice use of echo AFTER header() function
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 Tip 1: 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=https://nimishprabhu.com/">

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

Bonus Tip 2: Javascript code to redirect a webpage after X seconds.

If you would like to use Javascript for some reason, then you can use setTimeout() function to achieve similar result, provided user has Javascript enabled.

<script>
setTimeout(function (){ window.location.href= 'https://nimishprabhu.com';},5000);
</script>

You will replace “https://nimishprabhu.com” with your URL and 5000 (5000 milliseconds i.e 5 seconds) with desired number of milliseconds to wait before redirecting the page.

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

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

  1. Angela says:

    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

    1. NIMISH says:

      Hello Angela

      Check out these links:

      http://ubuntuforums.org/showthread.php?t=1937468

      http://stackoverflow.com/questions/10421471/redirect-html5-video-after-play

      They should be helpful.

      Thanks & Regards

      Nimish Prabhu

  2. A.Torres says:

    Thanks 💖

  3. Jol says:

    Thanks!!!!

  4. chuk says:

    Hi i cant use header location in this code, can you help me:

    <?php

    if (isset($_POST['email'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $subject = $_POST['subject'];
    $msg = $_POST['message'];

    $to = "s@hotmail.com";

    $message = "

    Meassage from ‘.$name.’

    Nombre: ‘.$name.’
    Telefono: ‘.$phone.’
    Asunto: ‘.$subject.’
    Mensaje: ‘.$msg.’

    “;

    // Always set content-type when sending HTML email
    $headers = “MIME-Version: 1.0” . “\r\n”;
    $headers .= “Content-type:text/html;charset=UTF-8” . “\r\n”;

    // More headers
    $headers .= “From: ” . $email . “\r\n”;
    $headers .= “Cc: ” . $email . “\r\n”;

    mail($to, $subject, $message, $headers);
    echo “Hemos recibido tu mail!”;

    }

    else {

    echo “Ocurrio un error”;

    }

    ?>

  5. NIMISH says:

    @chuk,
    $headers variable in your post contains headers for the email you are trying to send.
    To redirect user after email is successfully sent, you can include following:
    header(“Refresh: 5; url=index.php”);
    Use this in your if statement, before echo “Hemos recibido tu mail!”;
    Also make sure that you are not displaying any text before executing header() function.

Leave a Reply

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.