How to Add Registered Claims to a JWT in PHP with php-jwt
With firebase/php-jwt, the registered claims are ordinary keys in the payload array you pass to JWT::encode: iss, sub, aud, iat, nbf, exp and jti. The three times are Unix seconds, which time() gives you.
Syntax
$payload = [
'iss' => '<issuer>',
'sub' => '<subject>',
'aud' => '<audience>',
'iat' => <seconds>,
'nbf' => <seconds>,
'exp' => <seconds>,
'jti' => '<id>',
];
$token = JWT::encode($payload, <key>, '<alg>');
| Part | What it does |
|---|---|
'iss', 'sub', 'jti' | Strings: who made the token, who it's about and its own ID. |
'aud' | Who the token is for: one string, or an array of strings. |
<seconds> | A Unix time in seconds, such as time() or time() + 7200. |
JWT::encode(...) | Signs the payload with the key and returns the token. |
The php-jwt README builds its example payload the same way, with 'iss' => 'example.org', 'aud' => 'example.com', 'iat' and 'nbf' as keys of the array.
The Example
The example signs a token for a user named ada, issued by https://login.example.org for https://photos.example.org. It sets iat and nbf to now, exp two hours later, and jti to 16 random bytes written as hexadecimal. Then it decodes the token with JWT::decode and prints four of its claims.
Prerequisites
Step 1: Create the Project
mkdir demo
cd demo
composer require firebase/php-jwt
composer require installs php-jwt, version 7.2.2 here, and writes the autoloader the code loads.
Step 2: Write the Code
Create main.php:

<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
// The signing key: 32 random bytes, written as base64.
// It's in the code only for this example; a real app loads it from its configuration.
$key = base64_decode('C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64=', true);
$now = time();
$payload = [
'iss' => 'https://login.example.org',
'sub' => 'ada',
'aud' => 'https://photos.example.org',
'iat' => $now,
'nbf' => $now,
'exp' => $now + 2 * 60 * 60,
'jti' => bin2hex(random_bytes(16)),
];
$token = JWT::encode($payload, $key, 'HS256');
echo $token . "\n";
$claims = JWT::decode($token, new Key($key, 'HS256'));
echo "iss {$claims->iss}, aud {$claims->aud}, exp {$claims->exp}, jti {$claims->jti}\n";
JWT::decode returns the claims as an object, so each claim is a property such as $claims->iss.
Step 3: Run It
php main.php
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2xvZ2luLmV4YW1wbGUub3JnIiwic3ViIjoiYWRhIiwiYXVkIjoiaHR0cHM6Ly9waG90b3MuZXhhbXBsZS5vcmciLCJpYXQiOjE3OTYwNTk0NjYsIm5iZiI6MTc5NjA1OTQ2NiwiZXhwIjoxNzk2MDY2NjY2LCJqdGkiOiI2ZGU5Y2JmZjI0YmI5ZGEyNDBmMWQwNjliODNiMjRkYyJ9.I4B6BzQwVJvuD7lGKNFnqews-lGubREOOGSP_tmw_mE
iss https://login.example.org, aud https://photos.example.org, exp 1796066666, jti 6de9cbff24bb9da240f1d069b83b24dc

What Happened
- php-jwt wrote the array as the token's payload, in the order of its keys.
JWT::decodeaccepted the token: the signature matched,nbfandiatweren't in the future, andexpwasn't past.expistime() + 2 * 60 * 60, two hours in seconds.
JWT::decode checks exp, nbf and iat, but it doesn't check iss or aud. Code that relies on them has to compare them itself.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Writing times in milliseconds | The token looks valid for tens of thousands of years | Use time(), which is in seconds |
Leaving out exp | JWT::decode accepts the token forever | Always set an expiry |
Expecting JWT::decode to check aud | It doesn't, so a token for another API passes | Compare aud yourself after decoding |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Firebase\JWT\BeforeValidException: Cannot handle token with nbf prior to ... | nbf is in the future | Set nbf to now, or leave it out |
UnexpectedValueException: Payload exp must be a number | exp was a string or a date | Pass a number of seconds |
FAQ
Do I have to set all seven claims?
No. The JWT standard makes every registered claim optional. Set the ones your API checks, and always an expiry.
Why bin2hex(random_bytes(16)) and not a UUID?
PHP has no UUID function. Sixteen random bytes written as hexadecimal make an ID that's just as unlikely to repeat, which is all jti needs.
Conclusion
Registered claims in php-jwt are keys in the payload array, with Unix seconds for the times. JWT::encode signs them, and JWT::decode checks the times but leaves iss and aud to you.
Additional Resources
- php-jwt, the library's README.
- RFC 7519 section 4.1, the registered claim names.
