• Welcome to Valhalla Legends Archive.
 

header() error

Started by ChroniX, March 15, 2008, 07:11 PM

Previous topic - Next topic

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"

<?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");}?>


How do I fix this? I don't understand why it's sending the header before the if statement is even executed...

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.

ChroniX

#2
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.

<?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>';?>

FrostWraith

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

You are comparing a boolean to a stored variable silly.

ChroniX

But how do I resolve my problem, the script works just fine until I add in the else statement.

FrostWraith

Something along the lines of this.  It's been a while for php and I, but the logic will always be the same.

if (isset($_POST['password']))
{
    if ($_POST['password'] == $password)
    {
        header("Location: register.php");
    }
    else
    {
        header("Location: login.php");
    }
}

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?

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.

ChroniX

Ah alright, I understand now.  Well thank you for the help, and that brief tutorial. :D