如何实现记住我的功能? [英] How to implement remember me feature?

查看:148
本文介绍了如何实现记住我的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

每个用户都有唯一的32个字符id这: md5(salt。$ username。$ user_password。salt2); )。我将此值存储在表用户的unique_id字段下。这是一个很好的方法,将这个值分配给用户的cookie,并让他只有在他被分配的时候才登录?并且当然检查它如果该值存在于数据库中?

Every user has unique 32 chars id (made of like this: md5("salt" . $username . $user_password . "salt2");). And I store this value under 'unique_id' field in table users. Is it a good way to assign this value to user's cookie and let him be logged in only if he has it assigned? And of course check it if that value exists in database?

我认为这不是一个好的做法,因为如果有人窃取您的cookie,他们将能够登录您的帐户。

I don't think it's a a good practise, because if someone steals your cookie, they will be able to log in to your account.

其他/更好的解决方案是什么?当然最安全的事情可能只是存储在会话中,但我想实现这个记住我的功能。

What's the other/better solution? Of course the safest thing is probably just to store it in sessions, but I want to implement this remember me feature.

谢谢。

推荐答案

说明数据库表的持久性Cookie名称是 pcookies ,包含以下列:

Say database table's name for persistent cookie is pcookies with the following columns:


  • cookie_id(CHAR)

  • user_id(INT)

  • 到期日期:DATETIME

  • salt(CHAR)

  • cookie_id (CHAR)
  • user_id (INT)
  • expiry (DATETIME)
  • salt (CHAR)

Cookie创建步骤


  1. 成功登录后,在数据库中以唯一ID创建cookie记录。您可以通过hash_hmac('sha512',$ token,$ salt)生成它$ token = uniqid($ user_id,TRUE)和$ salt = md5(mt_rand())。


  2. 在Cookie中存储Cookie ID和令牌。
  3. 在Cookie中存储'用户ID','过期时间'和'salt' >
  1. After successful login, create a cookie record in database under an unique id. You may generate it by hash_hmac('sha512', $token, $salt) where $token=uniqid($user_id, TRUE) and $salt=md5(mt_rand()).
  2. Store 'user id', 'expiration time' and 'salt' along with the 'cookie id' in database.
  3. Store 'cookie id' and 'token' in cookie.

验证步骤


  1. 如果找到持久性Cookie,请首先检查记录是否在数据库中可用。

  2. 如果记录可用,请检查cookie是否到期。

  3. 如果Cookie未过期,则通过$ cookie_id == hash_hmac('sha512',$ token_from_cookie,$ salt_from_db)验证Cookie ID。

  4. 一次

  5. 如果找到的Cookie无效,请清除设备中的Cookie,然后删除该Cookie

  1. If there is a persistent cookie found, first check whether the record is available in database or not.
  2. If the record is available then check whether the cookie expires or not.
  3. If the cookie does not expire, then validate the cookie id by $cookie_id == hash_hmac('sha512',$token_from_cookie,$salt_from_db).
  4. Once the cookie is validated, delete it from database and create a new cookie according to the above cookie creation steps.
  5. If the cookie is found as invalid, then clear the cookie from the device and delete all other cookie records of the user from database, notice the use about a theft attempt and proceed to manual login process.

注意:


  • 会话可用时,忽略检查Cookie。


  • 不要允许用户执行敏感请求,例如密码更改或从持久性Cookie登录信息查看信用卡信息。调用密码登录并在会话中添加标志以允许所有正在进行的操作。

这篇关于如何实现记住我的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆