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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.