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怎么使用的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Stock Market GPT
人工智能驱动投资研究,做出更明智的决策

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

KOOK语音平台官网为https://www.kookapp.cn/,支持PC客户端及网页版登录,提供Windows、Android、iOS多端覆盖,用户可下载客户端或通过浏览器直接访问使用。

useunSerialize(serialize($ obj))fordeepcopyingwhenalldataiSerializable;否则,exhiment__clone()tomanallyDuplicateNestedObjectedObjectSandAvoidSharedReference。

usearray_merge()tocombinearrays,oftritingDupritingDuplicateStringKeySandReIndexingNumericKeys; forsimplerconcatenation,尤其是innphp5.6,usethesplatoperator [... $ array1,... $ array2]。

NamespacesinPHPorganizecodeandpreventnamingconflictsbygroupingclasses,interfaces,functions,andconstantsunderaspecificname.2.Defineanamespaceusingthenamespacekeywordatthetopofafile,followedbythenamespacename,suchasApp\Controllers.3.Usetheusekeywordtoi

当extervendingAclassInphp时,callparent :: __ construct()toensuretheparent'sinitializationLogicRuns.1.iftheparentClassConstructorSetSupessentialProperties,SkeippingThiscallMayResultInCompleteTeleteTeTeborteTepleteTebignEbigntInitialization.2.useparent.2.useceparent.2.usectectectectecon :: __ _______________________________)insideTheChild’sco

toupdateadatabaseRecordInphp,firstConnectusingpDoormySqli,thenusepreparedStatementStoExecuteAsecuteAsecuresqurupDatequery.example.example:$ pdo = newpdo(“ mySql:mysql:host = localHost; localhost; localhost; dbname; dbname = your_database = your_database',yous_database',$ username,$ username,$ squeaste;

usefileperms()togetFilePermissionsasanIntegerAntegatusingsPrintf('%o')
