PHP code example of horde / jwt

1. Go to this page and download the library: Download horde/jwt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

horde / jwt example snippets


use Horde\Jwt\Signer\Hs256Signer;
use Horde\Jwt\TokenDecoder;
use Horde\Jwt\TokenEncoder;
use Horde\Jwt\Verifier\Hs256Verifier;

$secret  = 'your-secret-key-at-least-32-bytes!';
$encoder = new TokenEncoder();
$decoder = new TokenDecoder();

// Sign
$token = $encoder->encode(['sub' => 'user123', 'iss' => 'myapp'], new Hs256Signer($secret));

// Verify
$verified = $decoder->decode($token->toString(), new Hs256Verifier($secret), [
    'verify_iss' => 'myapp',
]);

echo $verified->getSubject(); // 'user123'