create jwt token in php

Here's an example of how to create a JWT token in PHP using the firebase/php-jwt library:

  1. Install the firebase/php-jwt library using composer:
composer require firebase/php-jwt
34 chars
2 lines
  1. In your PHP code, import the firebase\JWT\JWT class:
main.php
use firebase\JWT\JWT;
22 chars
2 lines
  1. Set your secret and payload:
main.php
$secret_key = "my_secret_key";
$payload = array(
    "user_id" => 123,
    "email" => "john.doe@example.com"
);
112 chars
6 lines
  1. Encode the payload to create the token:
main.php
$jwt_token = JWT::encode($payload, $secret_key);
49 chars
2 lines

That's it! You now have a JWT token that can be sent to the client and used for authentication and authorization.

gistlibby LogSnag