PHP page redirect

669,355

Solution 1

Yes, you would use the header function.

/* Redirect browser */
header("Location: http://www.yourwebsite.com/user.php"); 
exit();

It is a good practice to call exit() right after it so that code below it does not get executed.

Also, from the documentation:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

This means you should not echo anything right before the header() function, as doing so will more than likely throw an error. Also, you will need to verify that this code gets run before any other output as well.

Solution 2

Using a javascript as a failsafe will ensure the user is redirected (even if the headers have already been sent). Here you go:

// $url should be an absolute url
function redirect($url){
    if (headers_sent()){
      die('<script type="text/javascript">window.location=\''.$url.'\';</script‌​>');
    }else{
      header('Location: ' . $url);
      die();
    }    
}

If you need to properly handle relative paths, I've written a function for that (but that's outside the scope of the question).

Solution 3

Simple way is to use:

  echo '<script>window.location.href = "the-target-page.php";</script>';

Solution 4

   $url='the/url/you/want/to/go';
   echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';

this works for me fine.

Solution 5

header( "Location: http://www.domain.com/user.php" );

But you can't first do an echo, and then redirect.

Share:
669,355

Related videos on Youtube

amit
Author by

amit

new to stack overflow. learning...

Updated on October 26, 2020

Comments

  • amit
    amit over 3 years

    Can PHP make a redirect call after executing a function? I am creating a function on the completion of which I want it to redirect to a file located in the same root folder. Can it be done?

    if (...) {
        // I am using echo here.
    } else if ($_SESSION['qnum'] > 10) { 
        session_destroy();
        echo "Some error occured.";
        // Redirect to "user.php".
    }
    
  • Gumbo
    Gumbo over 14 years
    Please use absolute URLs according to the HTTP specification.
  • stefando
    stefando over 14 years
    True, but make sure you remove the echo first or you might experience a "Headers allready send" ;)
  • brianreavis
    brianreavis over 14 years
    how descriptive.
  • Allain Lalonde
    Allain Lalonde over 14 years
    You should also set the HTTP Status to 302.
  • Gabe
    Gabe about 14 years
    Change this line to this and it will... die('<script type="text/javascript">window.location=\''.$url.'\';</script‌​>');
  • colithium
    colithium almost 14 years
    "The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set."
  • Nitzan Wilnai
    Nitzan Wilnai over 10 years
    The above function with Gabe's fix works!
  • Liza
    Liza over 8 years
    this works for me too. Thanks
  • Gellie Ann
    Gellie Ann over 7 years
    I'm bothered by "afterwords".
  • Naveed Ahmad
    Naveed Ahmad about 7 years
    Worked for me as well
  • SteveCinq
    SteveCinq about 7 years
    Yep, this was a life-saver for me; I dynamically populate page titles and body ids from a header include so no matter what, I was always going to have sent headers. And for me this is just a web form bot response. Nice one, thanks.
  • Prabal Thakur
    Prabal Thakur about 7 years
    this worked for me too. :) don't know why this is not at the top. :(
  • RozzA
    RozzA almost 7 years
    fine unless user has JS disabled.
  • Sam Denty
    Sam Denty over 6 years
    In which case you could use a meta redirect die('<meta http-equiv="refresh" content="0; url=' . $url . '" />');
  • Akash M
    Akash M about 6 years
    this worked for me also. Thank you.
  • Pete
    Pete almost 6 years
    Use die instead of exit to prevent only exiting the function.