tags:

views:

75

answers:

4

Grr. I have that problem AGAIN.

I really have no idea what the problem is :|, I don't know how to explain so here's my code:

    <?php
include("config.php");
$eUsername = $_POST['username'];
$ePassword = $_POST['password'];

$con = mysql_connect("localhost","MY_USERNAME","MY_PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("forum", $con);
$result = mysql_query("SELECT * FROM members WHERE username = '$eUsername'");

while($row = mysql_fetch_array($result))
  {
    if ($ePassword==$row['password']) {
      echo "Correct";
       setcookie("loggedIn", "true", time()+1000000000);
       setcookie("logUsername", "$eUsername", time()+100000000);
       setcookie("logPassword", "$ePassword", time()+100000000);
    }
    else {
      echo "Incorrect username/password.  Please try again.";
    }
  }
mysql_close($con);
if ($_COOKIE['loggedIn']=="true") {
$curURL=basename($_SERVER['SCRIPT_NAME']);
echo "You are already logged in.  <a href='$curURL?lo=true'>Log out?</a>";
}
echo "<br /><br />";
print_r($_COOKIE);
?>

So basically what this does is if you log in with the correct information, it will set three cookies, your username, password and one to check for the other two.

But when I do log in successfully, I get these errors:

Warning: Cannot modify header information - headers already sent by (output started at /home/scott/web/forum/index.php:18) in /home/scott/web/forum/index.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/scott/web/forum/index.php:18) in /home/scott/web/forum/index.php on line 20

Warning: Cannot modify header information - headers already sent by (output started at /home/scott/web/forum/index.php:18) in /home/scott/web/forum/index.php on line 21

:(

Somebody please help, thank you!! :)

-Scott

A: 

It looks like the issue is coming up when you set the session cookies. Perhaps you want to check if they exist before you set them, and if they do exist, take some other action.

Andrew
+3  A: 

You've got an echo which could occur before your setcookie call.

header or setcookie or anything else that sends HTTP headers has to be done before any other output, or you'll get that warning/error.

Also, you should check config.php to make sure there's no trailing whitespace after the closing ?> php tag. Remember... anything not inluded in <?php ... ?> is considered output by the php parser, and will get "echo'd" out.

Weston C
Thanks, man! It works!
Jaxo
+1  A: 

OK, this may be stupid -- I didn't even delve into your code -- but is this whitespace before the opening <?php? Also, I'd check config.php to ensure there's no whitespace outside the opening and closing tags as well.

chryss
A: 

You are writing "Correct" to the output stream before setting the cookies. When you start writing to the page, the HTML headers are written first. As the cookies goes in the header, you have to set them before starting to write to the page.

Guffa