Question:
Howcome in PHP, my Header("Location:") skips EVERYTHING?
anonymous
2010-11-18 18:43:51 UTC
Hi,

I have a php conditional, and if the conditional is met, it supposed to pop up a js dialog box, tell the user they failed, and then redirect the page with header ("location:") function. But it's really weird that once i put down the header function, once user submit the form that meets that conditional, it will directly redirect the page, without pop up the dialog box.

not even dialog box, it seems ignored all my previous conditionals, it just ignored everything ahead of it, seems it does not follow order of the lines anymore. it even changed my login button which is using another header function to redirect to another page. Something wrong with the submit buttons?

Here is part of the code:

if($emailcheck != 0)
{


?>






header( "Location: ./new_user.php" );


}



Thanks, Please help!!!
Three answers:
David Harris
2010-11-18 18:54:21 UTC
You cannot redirect if the headers have already been sent (i.e any input, etc)





try this:



if($emailcheck != 0)

{

print("");

print("");

}



redirect.php:




header("location: ./new_user.php");

?>



EDIT:

Yes, redirect has to be its own file. You cannot redirect if the headers have already been sent. Which is why you would use the HTML redirect than the PHP redirect.
just "JR"
2010-11-19 00:43:50 UTC
You cannot use the function header() after having output ANYTHING to the screen, not even a blank line or a space! This is a very common error made by beginners in Php.

In your case, you are outputing blank lines (spaces after { ), outputing some javascript (sent to the client machine), and an alert (displayed on the screen): you cannot use header() after any of these...
anonymous
2017-01-15 14:44:20 UTC
nicely, i ought to maximum probably attain the kitchen with a hop pass bounce combination, yet i'm donning socks and that i could in all risk slide around the floor and break into the oven. So i'm going to easily stay positioned and action picture star this question if that is happy with you.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...