acidjazz / summon
简单安全地处理用户会话
v1.4.3
2017-03-09 02:29 UTC
Requires
- php: >=5.6
README
简单安全地处理用户会话
这是什么
这是一个简单安全的设置cookie和恢复过期会话的方法,您可以根据需要保持会话活跃。它还允许您查看和控制用户的登录会话以及它们的来源。
它是如何工作的
- 当用户登录时,设置一些数据的编码字符串作为cookie。
- 在用户模型中存储内容以进行更好的验证
- 在正常会话过期后,允许您重新登录用户
特性
- 多浏览器/客户端支持
- 监控和控制多个会话
- 多级验证
- 验证cookie过期
- 验证浏览器代理(可选)
- 在数据库级别存储/验证我们的哈希
- 非昂贵的数据库查找
- 存储可索引的标识符以避免昂贵的用户查找
示例
在用户模型 $user 后登录
<? /* * sample code to: * - log the user in * - set a session cookie * - store that hash/encoded string in a Summon array in the user model * $results is an assoc array of * - 'token' set as a cookie (default named token) * - 'expires' when this session expires * - 'sessions' an update list of all the users sessions to store in the DB */ $results = Summon\Summon::set($user->id(true), $user->sessions); $user->sessions = $results['sessions']; $user->save();
检查用户是否已登录
<? /* sample function to to: - verify our cookies' validity - extract our cookies payload - verify once more in the DB */ public static function loggedIn() { if ($data = Summon\Summon::check()) { $user = new DBModelOfSomeSort\user($data['user_id']); if ($user->exists() && isset($user->sessions[$data['hash']])) { return $user; } } return false; }
删除会话,登出用户
<?php $user->summon = Summon\Summon::remove($user->summon); $user->save();
安装
- 修改您的用户表/集合以允许存储哈希=>字符串的小对象
- 在您的用户模型中存储 summon::set() 的结果(检查 login.php)
- 添加代码以验证过期会话,并具有潜在的重新登录功能(检查 check.php)
- 在登出区域添加代码以从您的用户模型中删除过期的哈希=>字符串(检查 logout.php)
- 添加一个定义为 "SUMMON_SECRET" 的唯一哈希/字符串值,并确保其安全
待办事项
- 在检查时删除过期/无效的召唤
- 基于代理等动态超时,例如平板电脑/手机应具有更短的过期时间
- 支持更多参数以用于数据库/索引目的的有效负载
为什么?
我花了很多时间在谷歌上搜索这种方法,足以预测这种东西需要存在。如果您有任何评论/想法/功能,请告诉我,或者更好的是,分叉它并提交拉取请求。