In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
这篇"PHP中ThinkPhp框架的token怎么使用"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"PHP中ThinkPhp框架的token怎么使用"文章吧。
一、token在ThinkPhp框架的使用
1. 首先在数据库的 users 表中添加两个字段token、time_out
token 用于存储用户的 token
time_out 用于设置用户 token 的过期时间
2.创建函数
checkToekn($token)
函数用于检验 token 是否存在, 并且更新 token。
public function checkToken($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) { return 90003; //token长时间未使用而过期,需重新登陆 } $new_time_out = time() + 604800; //604800是七天 $res = $user->isUpdate(true) ->where('token', $token) ->update(['time_out' => $new_time_out]); if ($res) { return 90001; //token验证成功,time_out刷新成功,可以获取接口信息 } } return 90002; //token错误验证失败}
3.创建函数
douserLogin($username,$password)
用于验证用户名密码, 并登陆, 返回 token 信息。
public function douserLogin() { $user = new \app\index\model\Users(); $userisset = $user->where('username', $username)->find(); if ($userisset == null) { return json_decode('{"user":"' . $username . '","code":"400","msg":"用户不存在"}'); } else { $userpsisset = $user ->where('username', $username) ->where('password', sha1(md5($password)))->find(); if ($userpsisset == null) { return json_decode('{"user":"' . $username . '","code":"401","msg":"密码错误"}'); } else { //session('user', $username); $token = $this->makeToken(); $time_out = strtotime("+7 days"); $userinfo = ['time_out' => $new_time_out, 'token' => $token]; $res = $user->isUpdate(true) ->where('username', $username) ->update($userinfo); if ($res) { return json_decode('{"user":"' . $username . '","toekn":'.$token.' "code":"0","msg":"登录成功"}'); } } }}
二、Token的概念
token是客户端频繁向服务器端请求数据,服务器频繁的去数据库查询用户名和密码判断用户名和密码正确与否,并作出相应的提示,在这样的背景下,token便应运而生了。
以上就是关于"PHP中ThinkPhp框架的token怎么使用"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.