This article is mainly to share with you the PHP implementation token of the example method hope to help everyone.
Public Function Set_token ($user _name) { $information [' state '] = false; $time = time (); $header = Array ( ' typ ' = ' JWT ' ); $array = Array ( ' iss ' = ' auth ',//rights verification author ' Iat ' = $time,//timestamp ' exp ' = ' = ',//token validity, 1 hours ' Sub ' = ' demo ',//Case ' user_name ' + $user _name )//user name ; $str = Base64_encode (Json_encode ($header)). '.' . Base64_encode (Json_encode ($array)); The array is converted into characters $str = UrlEncode ($STR);//transcoding via URL $information [' token '] = $STR; $this->save_token ($user _name, $information [' token ']); The user token is stored in the user database $information [' username '] = $user _name;//Returns the user name $information [' state '] = true; return $information;}
Annotations:
1, Function Save_token (), the user's current token is saved in the user table, for verifying
2, Base64_encode and Base64_decode cryptographic decryption function
(1) Encryption:
$str = ' www.php.cn '; Defines the string echo base64_encode ($STR); The output code is as follows: d3d3lmpinteubmv0ioiemuacros5i+wutg==
(2) Decryption:
$str = ' d3d3lmpinteubmv0ioiemuacros5i+wutg== '; Defines the string echo base64_decode ($STR); Output decoded content
3. Arrays and objects, convert JSON format, and invert
(1) Json_encode () is used to convert arrays and objects into JSON format:
$arr = Array (' A ' =>1, ' B ' =>2, ' C ' =>3, ' d ' =>4, ' e ' =>5); echo Json_encode ($arr); The result is: {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
(2) Json_decode () is used to convert JSON text to the appropriate PHP data structure:
$json = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} '; Var_dump (Json_decode ($json)); The result is a PHP object: Object (StdClass) #1 (5) {["a"] = = Int (1) ["B"] + int (2) ["C"] + int (3) ["D"] + int (4) ["e"] = = Int (5)}
4. Chinese processing
Echo UrlEncode ("Chinese-_."). " \ n "; %d6%d0%ce%c4-_.+ //convert Chinese text to urlencode text encoding echo urldecode ("%d6%d0%ce%c4-_."). " \ n "; Chinese-_. Convert urlencode text encoding to Chinese text
Public Function Set_token ($user _name) { $information [' state '] = false; $time = time (); $header = Array ( ' typ ' = ' JWT ' ); $array = Array ( ' iss ' = ' auth ',//rights verification author ' Iat ' = $time,//timestamp ' exp ' = ' = ',//token validity, 1 hours ' Sub ' = ' demo ',//Case ' user_name ' + $user _name )//user name ; $str = Base64_encode (Json_encode ($header)). '.' . Base64_encode (Json_encode ($array)); The array is converted into characters $str = UrlEncode ($STR);//transcoding via URL $information [' token '] = $STR; $this->save_token ($user _name, $information [' token ']); The user token is stored in the user database $information [' username '] = $user _name;//Returns the user name $information [' state '] = true; return $information;}
Annotations:
1, Function Save_token (), the user's current token is saved in the user table, for verifying
2, Base64_encode and Base64_decode cryptographic decryption function
(1) Encryption:
$str = ' Home of www.jb51.net script '; Defines the string echo base64_encode ($STR); The output code is as follows: d3d3lmpinteubmv0ioiemuacros5i+wutg==
(2) Decryption:
$str = ' d3d3lmpinteubmv0ioiemuacros5i+wutg== '; Defines the string echo base64_decode ($STR); Output decoded content
3. Arrays and objects, convert JSON format, and invert
(1) Json_encode () is used to convert arrays and objects into JSON format:
$arr = Array (' A ' =>1, ' B ' =>2, ' C ' =>3, ' d ' =>4, ' e ' =>5); echo Json_encode ($arr); The result is: {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
(2) Json_decode () is used to convert JSON text to the appropriate PHP data structure:
$json = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} '; Var_dump (Json_decode ($json)); The result is a PHP object: Object (StdClass) #1 (5) {["a"] = = Int (1) ["B"] + int (2) ["C"] + int (3) ["D"] + int (4) ["e"] = = Int (5)}
4. Chinese processing
echo urlencode ("Chinese-_."). " \ n "; %d6%d0%ce%c4-_.+//Convert Chinese text to urlencode text encoding echo urldecode ("%d6%d0%ce%c4-_."). " \ n "; Chinese-_. Convert urlencode text encoding to Chinese text