PHP custom token class and put the generated token in the authorization Code of the HTTP request header

Source: Internet
Author: User

This article brings the content is about PHP custom token class and will generate tokens placed in the HTTP request Header authorization code, there is a certain reference value, the need for friends can refer to, I hope to help you.

Today, when using Laravel to write something, you need to interact with the front end, look at the JWT (JSON Web token), and then try to write a simple token class that stores the member ID and member permissions in token, and when interacting with the front end, Place the generated tokens in the authorization of the HTTP request header, organized as follows:

<?phpnamespace App\http\controllers\auth;use Illuminate\http\request;use App\http\controllers\controller;class Tokencontroller extends controller{/** * Header * @var array */private static $header = ["    Type "=" "token", "alg" = "HS256"]; /** * Create payload * @param $memberId * @param $permission * @return Array */private static Func tion Payload ($memberId, $permission) {return ["ISS" = "http://api.creatshare.com" , "iat" = $_server[' request_time ', "exp" = $_server[' Request_ti                     ME '] + 7200, "GivenName" = "Creatshare", "MemberID" and "= $memberId,    "Permission" = $permission];        }/** * Encode data * @param $data * @return String */private static function encode ($data) { Return Base64_encode (Json_encode($data)); }/** * Generate a signature * @param $header * @param $payload * @param string $secret * @return S Tring */private static function signature ($header, $payload, $secret = ' secret ') {return Hash_hmac (' Sha    $header. $payload, $secret); }/** * Generate a token * @param $memberId * @param $permission * @return String */public stat                IC function Createtoken ($memberId, $permission) {$header = Self::encode (self:: $header);                $payload = Self::encode (self::p ayload ($memberId, $permission));                $signature = Self::signature ($header, $payload); Return $header. '. '. $payload. '.' .    $signature;  }/** * Check a token * @param $JWT * @param string $key * @return array|string */public static                function Checktoken ($JWT, $key = ' secret ') {$token = explode ('. ', $JWT); if (count ($token)! = 3) return 'Token invalid ';                 List ($header, $payload, $sign) = $token;                 if (Self::signature ($header, $payload)!== $sign) return ' token invalid ';                 $header = Json_decode (Base64_decode ($header), Json_object_as_array);                 $payload = Json_decode (Base64_decode ($payload), Json_object_as_array);                 if ($header [' type ']! = ' token ' | | $header [' ALG ']! = ' HS256 ') return ' token invalid '; if ($payload [' ISS ']! = ' http://api.creatshare.com ' | | $payload [' GivenName ']! = ' creatshare ') return ' t                 Oken invalid ';                 if (Isset ($payload [' exp ']) && $payload [' exp '] < time ()) return ' timeout '; return [' MemberID ' = $payload [' MemberID '], ' permission ' and ' = ' $payload [' permis    Sion ']; }/** * Get a token * @return NULL */public static functionGetToken () {$token = null;                if (Isset ($_server[' http_authorization ')) $token = $_server[' http_authorization '];    return $token; }}
 $token = Token::createtoken ($member _id, $member _permission);//Create a token$    token = Token::gettoken (); Get Token$result = Token::checktoken () from the HTTP request header; Parse token 

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.