位置: IT常识 - 正文

PHP中ThinkPhp框架的token使用(thinkphp框架怎么用)

编辑:rootadmin

推荐整理分享PHP中ThinkPhp框架的token使用(thinkphp框架怎么用),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:thinkphp3.2框架,thinkphp5.0框架,thinkphp框架设计原理,thinkphp框架设计原理,php thinkphp框架,thinkphp5.0框架,thinkphp3.2框架,thinkphp5.0框架,内容如对您有帮助,希望把文章链接给更多的朋友!

在做登录信息核对时,面对源源不断的数据比对,都会给服务器造成一定的压力。对于我们常用的ThinkPhp框架也会有这样的困扰。不过有一种方法可以解决这类问题,那就是Token的作用。

一、token在ThinkPhp框架的使用

1.首先在数据库的 users 表中添加两个字段token、time_out

PHP中ThinkPhp框架的token使用(thinkphp框架怎么用)

token 用于存储用户的 token

time_out 用于设置用户 token 的过期时间

2.创建函数

checkToekn($token)

函数用于检验 token 是否存在, 并且更新 token。

publicfunctioncheckToken($token){$user=new\app\index\model\Users();$res=$user->field('time_out')->where('token',$token)->select();if(!empty($res)){//dump(time()-$res[0]['time_out']);if(time()-$res[0]['time_out']>0){return90003;//token长时间未使用而过期,需重新登陆}$new_time_out=time()+604800;//604800是七天$res=$user->isUpdate(true)->where('token',$token)->update(['time_out'=>$new_time_out]);if($res){return90001;//token验证成功,time_out刷新成功,可以获取接口信息}}return90002;//token错误验证失败}

3.创建函数

douserLogin($username,$password)

用于验证用户名密码, 并登陆, 返回 token 信息。

publicfunctiondouserLogin(){$user=new\app\index\model\Users();$userisset=$user->where('username',$username)->find();if($userisset==null){returnjson_decode('{"user":"'.$username.'","code":"400","msg":"用户不存在"}');}else{$userpsisset=$user->where('username',$username)->where('password',sha1(md5($password)))->find();if($userpsisset==null){returnjson_decode('{"user":"'.$username.'","code":"401","msg":"密码错误"}');}else{//session('user',$username);$token=$this->makeToken();$time_out=strtotime("+7days");$userinfo=['time_out'=>$new_time_out,'token'=>$token];$res=$user->isUpdate(true)->where('username',$username)->update($userinfo);if($res){returnjson_decode('{"user":"'.$username.'","toekn":'.$token.'"code":"0","msg":"登录成功"}');}}}}

二、Token的概念

token是客户端频繁向服务器端请求数据,服务器频繁的去数据库查询用户名和密码判断用户名和密码正确与否,并作出相应的提示,在这样的背景下,token便应运而生了。

本文链接地址:https://www.jiuchutong.com/zhishi/304503.html 转载请保留说明!

上一篇:python操作微信客户端:WechatPCAPI库实现自动化回复(python 微信)

下一篇:mysql InnoDB的四种锁定范围(mysql ndb innodb)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络