Jump to content

Set cookie login


eevan79

Recommended Posts

I am not sure if following code working.

 

First I set cookie when user login:

//SET cookie
if (isset($_POST['rememberme'])) {
            /* Set cookie to last 1 year */
            setcookie('username', $_POST['user_name'], time()+60*60*24*365, '/account', '');
            setcookie('password', sha1($_POST['user_pass']), time()+60*60*24*365, '/account', '');
        } else {
            /* Cookie expires when browser closes */
            setcookie('username', $_POST['user_name'], false, '/account', '');
            setcookie('password', sha1($_POST['user_pass']), false, '/account', '');
        }
//END cookie

 

In always included header.php I check if this cookie exist:

if(isset($_COOKIE['username']) AND isset($_COOKIE['password']) AND $_SESSION['signed_in']==false) {
$result=mysql_query("SELECT * FROM ".$table_prefix."users WHERE user_name = '".$_COOKIE['username']."'");
if (mysql_num_rows($result) >=1)
{
$row = mysql_fetch_assoc ($result);
  if (sha1($row['user_pass']) == sha1($_COOKIE['password']))
  {
  echo "signed in";
  $_SESSION['signed_in'] = true;
  $_SESSION['user_id']     = $row['user_id'];
  $_SESSION['user_name'] = $row['user_name'];
  $_SESSION['user_level'] = $row['user_level'];
  $_SESSION['user_ip']     = $_SERVER["REMOTE_ADDR"];
  }
}
}

 

and when user signout:

setcookie('username', '', false, '/account', '');
setcookie('password', '', false, '/account', '');

 

I am not sure if this code working. How to check it (when session end?) ?

Link to comment
https://forums.phpfreaks.com/topic/208734-set-cookie-login/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.