I'm having problems with cookies. What i have is a login script, when it comes up affirmative (the user successfully log in) I want to store the username and password in a cookie for later use to check if the user is logged in throughout the site. Here's my code:
<?
if ($userID != "" && $passWord != ""){
function login($userID, $passWord) {
$conn = db_connect();
$result = mysql_query("SELECT * FROM login WHERE username='$userID' and password='$passWord'");
if(!$result)
return("Query could not be executed"); // if query error
if(mysql_num_rows($result)>0) {
// if the values match values in the database
return("true");
} else {
return("Invalid Username or Password");
}
}
}
if ($userID != "" && $passWord != ""){
// now check the values received via the form
$result = login($userID, $passWord);
if($result == "true") {
print("You have been logged in");
setcookie("siteuser",$userID, time()+3600);
setcookie("password",$passWord, time()+3600);
} else {
print($result);
}
}
?>
Now the problem is this. The code works except the cookies don't seem to want to work. The error message
"Warning: Cannot add header information - headers already sent by (output started /home/shabangw/public_html/test/login.php:23) in /home/shabangw/public_html/test/login.php on line 65"
So somethings obviously wrong with where I declared my cookies. How can I get it to input the cookies only if its a valid username and password?
Thanks