ThinkPHP3.2.2实现持久登录(记住我)功能的方法
实现持久登录,即用户在登录时,勾选了"记住我"之后,无论是否关闭浏览器,只要不退出登录,在指定的时间内始终保持登录状态(缺点是在另一台电脑上登录过后,之前那台电脑就不能继续保持登录状态)。
首先,持久登陆使用 cookie 实现,但是 cookie 中不能保存用户密码这样重要的信息,即使加密过。解决方案是在用户登录表中新建3个字段identifier:第二身份标识,token:永久登录标识,timeout:永久登录超时时间。
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| uname | varchar(20) | YES | | NULL | |
| upwd | varchar(20) | YES | | NULL | |
| uflag | int(11) | YES | | NULL | |
| identifier | varchar(32) | YES | | NULL | |
| token | varchar(32) | YES | | NULL | |
| timeout | int(11) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
在用户勾选了"记住我"登录时,应该生成一个唯一的 identifier,一个唯一的 token,并且设置一个过期时间 timeout,把两个代表身份的值写入cookie,设置 cookie 过期时间为 timeout,例如:setcookie('auth',"$identifier:$token",$timeout); 同时把三个值插入数据表;当用户再一次访问网站时,首先判断 cookie 中是否含有 auth,如果含有,则去数据库中进行身份比对(identifier 和 token),比对成功时,把用户信息写入 session,同时用户保持登录状态。
控制器 TestController.class.php
代码:
<?php
namespace Test\Controller;
use Think\Controller;
class TestController extends Controller {
public function login(){
//判断是否永久登录
$this->checkLong();
//已经登录则跳转至个人中心
if(isset($_SESSION['username'])){
$this->redirect('Test/ucenter');
}else{
//判断是否存在cookie
if(isset($_COOKIE['username'])){
$this->assign('username',$_COOKIE['username']);
}
//显示注册页
$this->display("test");
}
}
//显示验证码
public function verifyImg(){
$verify = new \Think\Verify();
//$verify->useZh = true; //使用中文验证码
$verify->length = 4;
$verify->entry();
}
//验证登录
public function check(){
$verify = new \Think\Verify();
if($verify->check(I("yzm"))){
//判断用户名密码
$user = new \Test\Model\TestModel();
$res = $user->checkName(I("username"),I("pwd"));
if($res === false){
echo "用户名或密码错误";
}else{
//用户信息存入session
session("username",$res['uname']);
session("id",$res['uid']);
//如果用户勾选了"记住我",则保持持久登陆
if(I("remember")){
$salt = $this->random_str(16);
//第二分身标识
$identifier = md5($salt . md5(I("username") . $salt));
//永久登录标识
$token = md5(uniqid(rand(), true));
//永久登录超时时间(1周)
$timeout = time()+3600*24*7;
//存入cookie
setcookie('auth',"$identifier:$token",$timeout);
$user->saveRemember($res['uid'],$identifier,$token,$timeout);
}
//把用户名存入cookie,退出登录后在表单保存用户名信息
setcookie('username',I('username'),time()+3600*24);
//跳转至会员中心
$this->redirect('Test/ucenter');
}
}else{
echo "输入错误";
}
}
//测试strstr函数
public function strstrtest(){
$param = "Think\Verify";
//第三个参数为true,返回'Think';没有第三个参数,返回'\Verify'
$name = strstr($param,'\\',true);
echo $name;
}
//用户中心
public function ucenter(){
//判断是否永久登录
$this->checkLong();
$this->assign("session",$_SESSION);
$this->display("ucenter");
}
//退出登录
public function loginout(){
session(null);
setcookie('auth', '', time()-1);
$this->redirect("Test/login");
}
//生成随机数,用于生成salt
public function random_str($length){
//生成一个包含 大写英文字母, 小写英文字母, 数字 的数组
$arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
$str = '';
$arr_len = count($arr);
for ($i = 0; $i < $length; $i++){
$rand = mt_rand(0, $arr_len-1);
$str.=$arr[$rand];
}
return $str;
}
//判断是否持久登录
public function checkLong(){
$check = new \Test\Model\TestModel();
$is_long = $check->checkRemember();
if($is_long === false){
}else{
session("username",$is_long['uname']);
session("id",$is_long['uid']);
}
}
}
模型 TestModel.class.php
<?php
namespace Test\Model;
use Think\Model;
class TestModel extends Model{
//验证登录信息
public function checkName($name,$pwd){
$admin = M("admin");
$info = $admin->getByUname($name);
if($info != null){
//验证密码
if($info['upwd'] == $pwd){
return $info;
}else{
return false;
}
}else{
return false;
}
}
//当用户勾选"记住我"
public function saveRemember($uid,$identifier,$token,$timeout){
$admin = M("admin");
$data['identifier'] = $identifier;
$data['token'] = $token;
$data['timeout'] = $timeout;
$where = " uid = ".$uid;
$res = $admin->data($data)->where($where)->save();
return $res;
}
//验证用户是否永久登录(记住我)
public function checkRemember(){
$arr = array();
$now = time();
list($identifier,$token) = explode(':',$_COOKIE['auth']);
if (ctype_alnum($identifier) && ctype_alnum($token)){
$arr['identifier'] = $identifier;
$arr['token'] = $token;
}else{
return false;
}
$admin = M("admin");
$info = $admin->getByidentifier($arr['identifier']);
if($info != null){
if($arr['token'] != $info['token']){
return false;
}else if($now > $info['timeout']){
return false;
}else{
return $info;
}
}else{
return false;
}
}
}
视图 登录页 test.html
<DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="__CONTROLLER__/check" method="post">
<if condition="$username neq null">
<input type="text" name="username" placeholder="用户名" value="{$username}"><br>
<else />
<input type="text" name="username" placeholder="用户名"><br>
</if>
<input type="password" name="pwd" placeholder="密码"><br>
<input type="text" name="yzm" placeholder="验证码"><img src="__CONTROLLER__/verifyImg" onClick="this.src=this.src+'?'+Math.random()"><br>
<input type="checkbox" name="remember" id="remember"><label for="remember">记住我</label>
<input type="submit" value="提交">
</form>
</body>
</html>
视图 个人中心 ucenter.html
<DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
</head>
<body>
<if condition="$session['username'] neq null">
<i>{$session.username},</i>
<else />
<i>游客,</i>
</if>
欢迎您<br>
<a href="__CONTROLLER__/loginout">退出登录</a>
</body>
</html>
ThinkPHP3.2.2实现持久登录(记住我)功能的方法的更多相关文章
- ThinkPHP 3.2.2 实现持久登录 ( 记住我 )
实现持久登录,即用户在登录时,勾选了"记住我"之后,无论是否关闭浏览器,只要不退出登录,在指定的时间内始终保持登录状态(缺点是在另一台电脑上登录过后,之前那台电脑就不能继续保持登录 ...
- Spring Security框架下实现两周内自动登录"记住我"功能
本文是Spring Security系列中的一篇.在上一篇文章中,我们通过实现UserDetailsService和UserDetails接口,实现了动态的从数据库加载用户.角色.权限相关信息,从而实 ...
- shiro 实现 网站登录记住我功能 学习记录(四)
在很多网站都有在登录的时候,比如说记住我 几天之内 只要再此打开这个网站,都不需要再登录的情况: 1.前台JSP增加 单选框:记住我 如 2.在处理登录的 Controller 代码中增加接收这个参 ...
- android 入门 005(登录记住)
android 入门 005(登录记住) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- SharedPreferences实现自动登录记住用户名密码
最近Android项目需要一个自动登录功能,完成之后,特总结一下,此功能依靠SharedPreferences进行实现. SharedPreferences简介 SharedPreferences ...
- session、cookie与“记住我的登录状态”的功能的实现
Cookie的机制 Cookie是浏览器(User Agent)访问一些网站后,这些网站存放在客户端的一组数据,用于使网站等跟踪用户,实现用户自定义功能. Cookie的Domain和Path属性标识 ...
- (转)session、cookie与“记住我的登录状态”的功能的实现
Cookie的机制 Cookie是浏览器(User Agent)访问一些网站后,这些网站存放在客户端的一组数据,用于使网站等跟踪用户,实现用户自定义功能. Cookie的Domain和Path属性标识 ...
- SpringBootSecurity学习(10)网页版登录之记住我功能
场景 很多登录都有记住我这个功能,在用户登陆一次以后,系统会记住用户一段时间,在这段时间,用户不用反复登陆就可以使用我们的系统.记住用户功能的基本原理如下图: 用户登录的时候,请求发送给过滤器User ...
- vue 登录 + 记住密码 + 密码加密解密
<template> <el-form :model="ruleForm"> <h3 class="title">系统登录& ...
随机推荐
- Web for pentester_writeup之SQL injections篇
Web for pentester_writeup之SQL injections篇 SQL injections(SQL注入) Example 1 测试参数,添加 and '1'='1, 'and ' ...
- Kong02-KongA 介绍
KongA 是 Kong 的一个 GUI 工具.GitHub 地址是 https://github.com/pantsel/konga . KongA 概述 KongA 带来的一个最大的便利就是可以很 ...
- Mybatis:配置解析
配置解析 mybatis-config.xml(Mybatis核心配置文件)深深影响了Mybatis行为的设置和属性信息. 能配置的的内容 当然,并不是所有都是我们经常使用到的,下面选择经常使用的配 ...
- if __name__ == "__main__" 的作用
作用:当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行. 例子: # file one.py def func(): print("func() in one.py& ...
- mine:dp
一个小的线性dp.方法很多,八仙过海各显神通. 我想讲一下我的: #include<cstdio> #define mod 1000000007 ];][][],n;//是不是雷,右边有没 ...
- python面试题2.1:如何实现栈
本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 2.1 如何实现栈 [阿里巴巴面试题] 难度系数:⭐⭐⭐ 考察 ...
- java遍历一个实体
//遍历order,得到属性值不为空的属性,type:操作类型.0是新增,1是更新 private Map<String, Object> reflect(Order order,Stri ...
- lufylegend.js教程(1)
1.图片元素如何缩小? 在LSprite类中,有两个属性:{scaleX,scaleY},这两个属性属于按比例缩放精灵对象,可以放大,可以缩小,注意这两个属性是在图片中心点位置开始缩放. 代码: Bo ...
- (二)初识NumPy库(数组的操作和运算)
本章主要介绍的是ndarray数组的操作和运算! 一. ndarray数组的操作: 操作是指对数组的索引和切片.索引是指获取数组中特定位置元素的过程:切片是指获取数组中元素子集的过程. 1.一维数组的 ...
- C#怎么实现文件下载功能的四种方法
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...