function login($username, $password){
$username = addslashes($username);
$password = md5($password);
echo "SELECT * FROM userstbl WHERE user_name='$username' AND password='$password'";
$result = mysql_query("SELECT * FROM userstbl WHERE user_name='$username' AND password='$password'");
if (mysql_num_rows($result) == 1){
$info = mysql_fetch_array($result);
$userid = $info[user_id];
$sessionid = md5($userid.time());
$time = time();
@setcookie('mycookie', $sessionid, $time+3600, '/', '');
echo '*page cookie='.$_COOKIE['mycookie'].'*';
mysql_query("DELETE FROM sessionstbl WHERE user_id='$userid'");
mysql_query("INSERT INTO sessionstbl (session_id,user_id,timestamp) VALUES ('$sessionid','$userid','$time')");
return $userid;
}else{
return 0;
}
};
function status(){
$sessionid = $_COOKIE[mycookie];
$oldtime = time() - 3600;
echo "status SELECT * FROM sessionstbl WHERE session_id='$sessionid' AND timestamp>$oldtime";
$result = mysql_query("SELECT * FROM sessionstbl WHERE session_id='$sessionid' AND timestamp>$oldtime");
if (mysql_num_rows($result) == 1){
$info = mysql_fetch_array($result);
return $info[userid];
};
return 0;
};
Just started to code user login pages and I have found the above login function on the net. The '@setcookie' part seems not working. Where I expect to see sessionid value, the echo line immediately after, gives me
page cookie=
The INSERT works with what looks like a big number in session table.
Can someone tell me what is wrong with this @setcookie code?
Result is when I check login status the 'Status' function never finds the session record!!
Also, this code creates it's own sessionid value (the md5() bit)? Doesn't php auto create a session id?
thanks