PDA

View Full Version : cookie problem


Roost3r
10-30-2002, 08:55 PM
im getting this error: Warning: Cannot add header information - headers already sent by (output started at /home/cklepeis/public_html/login.php:10) in /home/cklepeis/public_html/login.php on line 105

line 105 is: setcookie("xhcookie", $mahname, time() - 3600);



<?php
$db = mysql_connect("localhost", "bleh", "bleh");
mysql_select_db("xhdb",$db);
if ($submit)
{
$getmyname = mysql_query("Select * from users where name ='$mahname'", $db) or die("Login system error, please report this to the webmaster.");
if (mysql_num_rows($getmyname) == 0)
{
echo "Incorrect username.";
}
else
{
while ($checkpass = mysql_fetch_array($getmyname)) {
$myname = $checkpass["user"];
$mypassword = $checkpass["psswd"];
$mystatus = $checkpass["status"];
if ($mypassword == $mahpassword)
{
echo "Thank you for logging in ".$mahname;
setcookie("xhcookie", $mahname, time() - 3600);
}
else
{
echo "Wrong password";
}
}
}
}
else {
?>
<center><b><u>Login</u></b><br><br>
<FORM ACTION="<?=$PHP_SELF?>" METHOD="POST">
UserName: <INPUT TYPE="text" NAME="mahname" size="15" maxlength="15"><br>
Password: &nbsp;<INPUT TYPE="password" NAME="mahpassword" SIZE="15" maxlength="15"><br>
<INPUT TYPE="submit" value="Submit" name="submit">
</FORM>
Forget your information? <a href="login.php">Email my info</a><br>
<a href="register.php">Register</a>
</center>
<?php
}
?>


thanks

Socraties
10-30-2002, 09:16 PM
a good work around to this is using the ob_start() and ob_end_flush() functions. These functions essentially stop loading the page and let you add values that should have been added in the header information. So try it this way.

ob_start();
setcookie("xhcookie", $mahname, time() - 3600);
ob_end_flush();

This should let you expire your cookie.

Roost3r
10-30-2002, 09:56 PM
didnt work; thanks anyway

Roost3r
10-30-2002, 11:27 PM
well i was told that i cant have any echo or prints before the set cookie; so i deleted tem and i still get that smae error; please help me this is a urgent problem

any help will be appreciated; all i need to do is have a form and get the username and pazssword off it then check it against my database; if succesful set a cookie

Nightfire
10-31-2002, 01:48 PM
If you have any html before setcookie, you will get an error. setcookie has to be before any html is shown.

kalm
10-31-2002, 01:50 PM
I have noticed another problem you are setting your cookie to expire in the past rather than in the future.

This could cause a problem once the script is working as you won't have a cookie for othe pages to user to check if you are logged in.

Chris Hunter
10-31-2002, 02:46 PM
lol- i did that cookie-past-setting fault myself two days ago. i am really happy to see that i am not the only one *g*