Valhalla Legends Forums Archive | Web Development | header() error

AuthorMessageTime
Chronix
When I run the following script in my browser, I get this error: "Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\register_login.php:8 ) in C:\Program Files\Apache Group\Apache2\htdocs\register_login.php on line 14"

[code]<?php

$password = "*";

echo '<form name="register_login" action"'.$_SERVER['PHP_SELF'].'" method="post">
<input type="password" name="password">
<input type="submit" name="submit" value="Authenticate">
</form>';

if (isset($_POST['password']) && $_POST['password'] == $password)

{

header("Location: register.php");

}

?>[/code]

How do I fix this? I don't understand why it's sending the header before the if statement is even executed...
March 16, 2008, 12:11 AM
K
You can't use the header() function once data has already been sent.  header() has to come before any HTML or php output code, including blank lines and such.
March 16, 2008, 12:29 AM
Chronix
So I can put it after the variable and before echo, then it should execute with no problem?

Alright, well that worked, but now I'm having another problem and need a push in the right direction.  The script works out exactly as I want it to until I add the else statement telling it to redirect too login.php if the password is incorrect.  After adding the else statement, and refreshing register_login.php, the page automatically redirects to login.php without warning. ???  Here is my code.

[code]<?php

$password = "halo122188";

if (isset($_POST['password']) == $password)

{

header("Location: register.php");

}

else

{

header("Location: login.php");

}

echo '<form name="register_login" action"'.$_SERVER['PHP_SELF'].'" method="post">
<input type="password" name="password">
<input type="submit" name="submit" value="Authenticate">
</form>';

?>[/code]
March 16, 2008, 12:31 AM
FrostWraith
isset($_POST['password']) == $password)

You are comparing a boolean to a stored variable silly.
March 16, 2008, 12:54 AM
Chronix
But how do I resolve my problem, the script works just fine until I add in the else statement.
March 16, 2008, 1:14 AM
FrostWraith
Something along the lines of this.  It's been a while for php and I, but the logic will always be the same.
[code]
if (isset($_POST['password']))
{
    if ($_POST['password'] == $password)
    {
        header("Location: register.php");
    }
    else
    {
        header("Location: login.php");
    }
}
[/code]
March 16, 2008, 1:21 AM
Chronix
Ah, your a genius, thank you!  Just for the record, and to make this a learning experience so I can't just say I bummed the answer without learning anything, where does the second if statement come into play, why was it required?
March 16, 2008, 1:38 AM
FrostWraith
Well, when you call the function isset(), it either returns true or false.
What you were doing was testing if true or false was equal to your password, which will never be the case.
So the first if statement verifies that there is a cookie set under that name, and the second if statement actually checks to see what value is stored in the cookie.
March 16, 2008, 1:50 AM
Chronix
Ah alright, I understand now.  Well thank you for the help, and that brief tutorial. :D
March 16, 2008, 2:15 AM

Search