I have a login script that validates a user and sets cookies. The login works fine. The problem is that the cookie does not seem to be saving. I don't see the cookie with my other cookies and when I use print_r($_COOKIE) it results in Array ()
Here's the login script
//A file with the database host,user,password, and selected database
require_once ('db.inc');
//A string used for md5 encryption.
$supersecret_hash_padding = 'a string that is used to pad out short strings for md5 encryption.';
$LOGGED_IN = false;
unset($LOGGED_IN);
/**
* Checks to see if the user is logged in
*
* @param integer $LOGGED_IN
* @email string $email e-mail address entered at login
*/
function user_isLoggedIn(){
//This function will only work with superglobal arrays, because
//I'm not passing in any values or declaring globals
global $supersecret_hash_padding, $LOGGED_IN;
//Have we already run the hash checks?
//If so, return the pre-set var
if (isSet($LOGGED_IN)){
return $LOGGED_IN;
}
if ($_COOKIE['email'] && $_COOKIE['id_hash']){
$hash = md5($_COOKIE['email'].$supersecret_hash_padding);
if ($hash == $_COOKIE['id_hash']){
return true;
}else{
return false;
}
}else{
return false;
}
}
function user_login(){
//This function will only work with superglobal arrays, because
//I'm not passing in any values or declaring glbals
dbConnect('crc1');
if (!$_POST['email'] || !$_POST['password']){
$feedback = 'ERROR- Missing e-mail address or password';
return $feedback;
}else{
$email = strtolower($_POST['email']);
//Don't need to trim because extra spaces should fail for this
//Don't need to addslashes because single quotes aren't allowed
$password = strtolower($_POST['password']);
//Don't need to addslashes because we'll be hashing it
$crypt_pwd = md5($password);
$qValidate = "SELECT firstname,lastname,status_id
FROM tblusers
WHERE emailaddress = '$email'
AND password = '$crypt_pwd'";
$result = mysql_query($qValidate);
if($result >0){
while($row=mysql_fetch_array($result)){
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$space = ' ';
$fullname = $firstname.$space.$lastname;
if (!$result || mysql_num_rows($result) < 1){
$feedback = 'ERROR- User not found or password incorrect';
return $feedback;
}else{
if (mysql_result($result,0,'status_id') =='1'){
user_set_tokens($email);
return 1;
}else{
$feedback = 'ERROR- You may not have confirmed your account yet';
return $feedback;
}
}
}
}
}
}
function user_logout(){
setcookie('email','',(time()+2592000),'/','',0);
setcookie('id_hash','',(time()+2592000),'/','',0);
}
function user_set_tokens($email_in){
global $supersecret_hash_padding;
if (!$email_in){
$feedback = 'No username';
return false;
}
$email = strtolower($email_in);
$id_hash = md5($email.$supersecret_hash_padding);
setcookie('email',$email,(time()+2592000),'/','localhost');
setcookie('id_hash',$id_hash,(time()+2592000),'/','localhost');
}
?>
On the next page I call the cookie email using $email = $_COOKIE['email'], but I keep getting undefined index: email.
Any thoughts on how I can get this to work? Thanks.