PHP implements Token instance method

小云云
Release: 2023-03-21 13:08:01
Original
20618 people have browsed it

This article mainly shares with you the example methods of implementing Token in PHP, hoping to help everyone.

public function set_token($user_name){ $information ['state'] = false; $time = time(); $header = array( 'typ' => 'JWT' ); $array = array( 'iss' => 'auth', // 权限验证作者 'iat' => $time, // 时间戳 'exp' => 3600, // token有效期,1小时 'sub' => 'demo', // 案例 'user_name' => $user_name ) // 用户名 ; $str = base64_encode(json_encode($header)) . '.' . base64_encode(json_encode($array)); // 数组转成字符 $str = urlencode($str); // 通过url转码 $information ['token'] = $str; $this->save_token($user_name, $information ['token']); // 将用户token存放进用户数据库 $information ['username'] = $user_name; // 返回用户名 $information ['state'] = true; return $information; }
Copy after login

Notes:
1. Function save_token(), saves the user's current token in the user table for verification
2. Base64_encode and base64_decode encryption and decryption functions
(1 ) Encryption:

$str='m.sbmmt.com'; //定义字符串 echo base64_encode($str); // 输出编码后的内容为: d3d3LmpiNTEubmV0IOiEmuacrOS5i+Wutg==
Copy after login

(2) Decryption:

$str='d3d3LmpiNTEubmV0IOiEmuacrOS5i+Wutg=='; //定义字符串 echo base64_decode($str); //输出解码后的内容
Copy after login

3. Arrays and objects, convert json format, and reverse
(1) json_encode() is used to convert arrays and objects , converted to json format:

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);   echo json_encode($arr);  结果为: {"a":1,"b":2,"c":3,"d":4,"e":5}
Copy after login
Copy after login

(2)json_decode() is used to convert json text into the corresponding PHP data structure:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); 结果就是生成一个PHP对象: object(stdClass)#1 (5) {     ["a"] => int(1)     ["b"] => int(2)     ["c"] => int(3)     ["d"] => int(4)     ["e"] => int(5)   } 
Copy after login
Copy after login

4. Chinese processing

echo urlencode("中文-_. ")."\n"; //%D6%D0%CE%C4-_.+ //将中文文字转换成urlencode文字编码 echo urldecode("%D6%D0%CE%C4-_. ")."\n"; //中文-_. //将urlencode文字编码转换成中文文字
Copy after login
Copy after login

public function set_token($user_name){ $information ['state'] = false; $time = time(); $header = array( 'typ' => 'JWT' ); $array = array( 'iss' => 'auth', // 权限验证作者 'iat' => $time, // 时间戳 'exp' => 3600, // token有效期,1小时 'sub' => 'demo', // 案例 'user_name' => $user_name ) // 用户名 ; $str = base64_encode(json_encode($header)) . '.' . base64_encode(json_encode($array)); // 数组转成字符 $str = urlencode($str); // 通过url转码 $information ['token'] = $str; $this->save_token($user_name, $information ['token']); // 将用户token存放进用户数据库 $information ['username'] = $user_name; // 返回用户名 $information ['state'] = true; return $information; }
Copy after login

Notes:
1. Function save_token(), saves the user's current token in the user table for verification
2. Base64_encode and base64_decode encryption and decryption functions
(1 ) Encryption:

$str='www.jb51.net 脚本之家'; //定义字符串 echo base64_encode($str); // 输出编码后的内容为: d3d3LmpiNTEubmV0IOiEmuacrOS5i+Wutg==
Copy after login

(2) Decryption:

$str='d3d3LmpiNTEubmV0IOiEmuacrOS5i+Wutg=='; //定义字符串 echo base64_decode($str); //输出解码后的内容
Copy after login

3. Arrays and objects, convert json format, and reverse
(1) json_encode() is used to convert arrays and objects , converted to json format:

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);   echo json_encode($arr);  结果为: {"a":1,"b":2,"c":3,"d":4,"e":5}
Copy after login
Copy after login

(2)json_decode() is used to convert json text into the corresponding PHP data structure:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); 结果就是生成一个PHP对象: object(stdClass)#1 (5) {     ["a"] => int(1)     ["b"] => int(2)     ["c"] => int(3)     ["d"] => int(4)     ["e"] => int(5)   } 
Copy after login
Copy after login

4. Chinese processing

echo urlencode("中文-_. ")."\n"; //%D6%D0%CE%C4-_.+ //将中文文字转换成urlencode文字编码 echo urldecode("%D6%D0%CE%C4-_. ")."\n"; //中文-_. //将urlencode文字编码转换成中文文字
Copy after login
Copy after login

Related recommendations:

Solution to WeChat development Token verification failure

How to solve the access_token global cache problem in singleton mode

PHP WeChat public account verification token, reply content, push message method

The above is the detailed content of PHP implements Token instance method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!