tags:

views:

80

answers:

3

I know in the Manuel it says that the header has to be the first thing in a script, but how come I see some codes where header("Location: member.php?id=$username") is in a if-statement?

Ex:

//a bunch of codes above

if($result!="0"){ 
// authenication correct lets login
$_SESSION["password"] = $password;;
$_SESSION["username"] = $username;
header("Location: member.php?id=$username");
}
else 
{
    echo "Wrong username or password. Please try again!";
}

But when I do this, it sometimes would/won't throw an error. How do I allow the header (); to be used in a script without any errors? I want to redirect the user back to the login if they click "no" and to the homepage if they click "yes".

+7  A: 

It doesn't have to be the first thing in the script. But it haves to be the first thing that you output to the user. You MUST NOT echo stuff before using the header function. If you don't, you can use it at any place you want.

You could also "ignore output" using ob_start and ob_end_clean.

Best regards,
T.

Thiago Silveira
So I can't use ANY echo statements? What if I wanted to echo the username or password was incorrect? Because if I put the header before the echos, they wont see the message.
ggfan
You can use echo statements after every header function call has been made or if you don't need to redirect the user. In your example, you should not use any echo statement before the header("Location: member.php?id=$username");, but you can use them after that call.
Thiago Silveira
But the problem is if I put the header before any echo statements, it would redirect the user without them seeing the messages :/
ggfan
Yes, if you want to display the messages, you should pass that to the script that's going to receive that redirection. Example: login.php?error=wrong_pw. Then you parse the $_GET["error"] variable and display messages accordingly. It is recommended that you use error codes, though, because if you use direct error messages, anyone can "edit what your site displays". (although is just for them)
Thiago Silveira
THe user never sees any output when a `Location:` header is received. The browser doesn't output anything when it receives a location header. It just navigates to the target page immediately. If you want to display a message before redirecting, you have to use a meta redirect or javascript.
Frank Farmer
awesome thank you!
ggfan
A: 

You have to use output buffering so that nothing is sent back to the browser until the entire operation is complete. Check out the ob_start function, that should give you a good starting place.

ob_start

Without output buffering, the script sends header info back to the browser, and once that has happened you cannot use header() to redirect.

jaywon
You don't have to use output buffering, but it's an option if you need to change headers late in the script. In his example, it's not needed
Michael Mrozek
Depends what "//a bunch of codes above" in his script does :)... But good point, You are correct if no output has been sent to that point, you don't need output buffering.
jaywon
+1  A: 

See my answer to a similar question.

fireeyedboy
The answer given in the link is very good.
chris