PHP page redirect
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.
Related videos on Youtube
Comments
-
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". }
-
user1972007 over 11 yearsI serach about this and i find related this answer in stackoverflow.com/questions/13539752/redirect-function/13539808
-
-
Gumbo over 14 yearsPlease use absolute URLs according to the HTTP specification.
-
stefando over 14 yearsTrue, but make sure you remove the echo first or you might experience a "Headers allready send" ;)
-
brianreavis over 14 yearshow descriptive.
-
Allain Lalonde over 14 yearsYou should also set the HTTP Status to 302.
-
Gabe about 14 yearsChange this line to this and it will...
die('<script type="text/javascript">window.location=\''.$url.'\';</script>');
-
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 over 10 yearsThe above function with Gabe's fix works!
-
Liza over 8 yearsthis works for me too. Thanks
-
Gellie Ann over 7 yearsI'm bothered by "afterwords".
-
Naveed Ahmad about 7 yearsWorked for me as well
-
SteveCinq about 7 yearsYep, 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 about 7 yearsthis worked for me too. :) don't know why this is not at the top. :(
-
RozzA almost 7 yearsfine unless user has JS disabled.
-
Sam Denty over 6 yearsIn which case you could use a meta redirect
die('<meta http-equiv="refresh" content="0; url=' . $url . '" />');
-
Akash M about 6 yearsthis worked for me also. Thank you.
-
Pete almost 6 yearsUse
die
instead ofexit
to prevent only exiting the function.