The following is the tutorial column of thinkphp to introduce [Example] thinkphp5 framework API token authentication function. I hope it will be helpful to friends in need!
Instructions for use: Generate token and refresh_token when logging in, and return them to the client. The client receives and saves local localStorage, etc., and brings the token every time it accesses the interface. The backend verifies that the token exists and is consistent before the next action can be performed. If it does not exist, it returns that the token has expired. The client calls the refresh interface and passes in the token and refresh_token. The server verifies, and the verification saves the database by regenerating a new token and returns Refresh local token access for the client to continue. When refresh_token verification fails, clear the database token, expiration time and other information
Simple token generation function (public function file common)
function create_token($id,$out_time){ return substr(md5($id.$out_time),5,26); }
Verify login method (model)
public function checkLogin($username,$passwd){ $driver = self::field('driver_id,passwd')->where('zhanghao',$username)->whereOr('phone',$username)->find(); if (empty($driver)){ $this->error = '账号不存在'; return false; } if ($driver['passwd'] != md5($passwd)){ $this->error = "密码不正确"; return false; } //$out_time = strtotime('+ 1 days'); $out_time = strtotime('+ 1 minutes'); $token = create_token($driver['driver_id'],$out_time); if(false===self::save(['token'=>$token,'time_out'=>$out_time],['driver_id'=>$driver['driver_id']])){ $this->error = '登陆失败'; return false; } $refresh_token_out_time = strtotime('+ 5 days'); $refresh_token = create_token($driver['driver_id'],$refresh_token_out_time); Cache::set("token",$token,60); Cache::set("driver_id",$driver['driver_id'],$refresh_token_out_time);//设置ID的过期时间和更新token的token时间一样用于更新的时候获取用户信息 Cache::set('refresh_token',$refresh_token,$refresh_token_out_time); return ['token'=>$token,'refresh_token'=>$refresh_token,'in_expire'=>$out_time]; }
token refresh method (model)
public function refreshToken($refresh_token,$token){ if (!isset(Cache::get('refresh_token')) or Cache::get('refresh_token')!=$refresh_token){ $this->error = '刷新token失败'; return false; } $cache_driver_id = Cache::get('driver_id'); $driver = self::field('driver_id,passwd')->where('driver_id',$cache_driver_id)->where('token',$token)->find(); if (empty($driver)){ $this->error = '参数错误'; return false; } $out_time = strtotime('+ 1 days');//新的过期时间 $token = create_token($driver['driver_id'],$out_time);//更新token if(false===self::save(['token'=>$token,'time_out'=>$out_time],['driver_id'=>$driver['driver_id']])){ Cache::clear($token); $this->error = '刷新失败'; return false; } Cache::set("token",$token,864000); return ['token'=>$token,'in_expire'=>$out_time]; }
Exit method (model)
public function logout($token,$refresh_token=''){ $driver = self::field('driver_id,passwd')->where('token',$token)->find(); self::save(['token'=>'','time_out'=>''],['token'=>$token]); Cache::clear('token'); Cache::clear('refresh_token'); }
The above is the detailed content of thinkphp5 framework API token authentication function [example]. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software