Home > PHP Framework > ThinkPHP > How to use the token of ThinkPhp framework in PHP

How to use the token of ThinkPhp framework in PHP

PHPz
Release: 2023-05-26 14:14:25
forward
1966 people have browsed it

1. The use of token in the ThinkPhp framework

1. First, add two fields token and time_out in the users table of the database

token is used to store the user's token

time_out is used to set the expiration time of the user token

2. Create a function

checkToekn($token)
Copy after login

Function is used for verification Whether the token exists, and update the 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错误验证失败
}
Copy after login

3. Create function

douserLogin($username,$password)
Copy after login

to verify username and password, log in, and return token information.

  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":"登录成功"}');
                }
            }
        }
}
Copy after login

2. The concept of Token

Token is when the client frequently requests data from the server, and the server frequently queries the database for the username and password to determine whether the username and password are correct. No, and make corresponding prompts. In this context, token came into being.

The above is the detailed content of How to use the token of ThinkPhp framework in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template