Last active
June 18, 2019 17:55
-
-
Save osor1o/a7a49da5fac9520808cff969976cc5c1 to your computer and use it in GitHub Desktop.
Create a JWT (Json Web Token) with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function encodeBase64Url(string $data) { | |
| return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); | |
| } | |
| $secret = "secret"; | |
| $header = encodeBase64Url(json_encode([ | |
| "alg" => "HS256", | |
| "typ" => "JWT" | |
| ])); | |
| $payload = encodeBase64Url(json_encode([ | |
| "id" => 122, | |
| "username" => "test" | |
| ])); | |
| $signature = encodeBase64Url(hash_hmac('sha256', "{$header}.{$payload}", $secret, true)); | |
| $jwt = "{$header}.{$payload}.{$signature}"; | |
| print $jwt . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment