Example method for implementing token in PHP

Source: Internet
Author: User
Tags urlencode
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 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.