Skip to main content

How to Decode Base64url in PHP with JWT::urlsafeB64Decode

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Base64url text, such as a piece of a JWT, uses - and _ where base64 uses + and /, and leaves out the padding. PHP's base64_decode doesn't know that and, in its default mode, silently drops the characters it doesn't recognise. php-jwt's JWT::urlsafeB64Decode converts the text first, so it decodes correctly.

Syntax​

Syntax
use Firebase\JWT\JWT;

$<decoded> = JWT::urlsafeB64Decode(<text>);
PartWhat it does
JWT::urlsafeB64Decode(<text>)Adds the missing = padding, swaps - and _ back to + and /, then decodes with base64_decode.
<decoded>The decoded bytes, as a string.

php-jwt's source documents urlsafeB64Decode as: "Decode a string with URL-safe Base64." The PHP manual for base64_decode explains the other half: without its strict mode, "invalid characters will be silently discarded."

The Example​

The example decodes eyJuIjoifn5-Pz4ifQ, the base64url form of a short piece of JSON. It contains a - and has no padding. The code decodes it once with JWT::urlsafeB64Decode and once with plain base64_decode, and shows both results with var_dump.

Prerequisites​

  • PHP and Composer. This guide uses PHP 8.6.0 and Composer 2.11.3.

Step 1: Create the Project​

Terminal
mkdir demo
cd demo
composer require firebase/php-jwt

composer require downloads php-jwt, version 7.2.2 here, into a vendor folder and writes vendor/autoload.php.

Step 2: Write the Code​

Create main.php in the demo folder:

main.php, which loads the autoloader, imports Firebase\JWT\JWT, and var_dumps JWT::urlsafeB64Decode and base64_decode of eyJuIjoifn5-Pz4ifQ.

main.php
<?php

require __DIR__ . '/vendor/autoload.php';

use Firebase\JWT\JWT;

// base64url of a short JSON object. It contains a - and has no padding.
$encoded = 'eyJuIjoifn5-Pz4ifQ';

var_dump(JWT::urlsafeB64Decode($encoded));
var_dump(base64_decode($encoded));

Step 3: Run It​

Terminal
php main.php
Output
string(13) "{"n":"~~~?>"}"
string(12) "{"n":"~~Oψ�"

A zsh terminal where php main.php prints string(13) with the JSON n equals three tildes, question mark and greater than, then string(12) with garbled characters.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • JWT::urlsafeB64Decode gave back the 13 bytes of the original JSON.
  • base64_decode dropped the - it didn't recognise, decoded what was left, and returned 12 bytes of which the last few are wrong. It reported no error.

Common Mistakes​

MistakeWhat happensDo this instead
Decoding base64url with plain base64_decodeCorrect only by luck, when the text has no - or _; wrong otherwise, with no errorUse JWT::urlsafeB64Decode
Expecting an exception for invalid inputJWT::urlsafeB64Decode('***') returned an empty string, though its docblock mentions InvalidArgumentExceptionCheck the result before using it
Trusting a decoded JWT payloadDecoding doesn't verify anythingVerify the token with JWT::decode and the key first

Troubleshooting​

SymptomLikely causeFix
A decoded value that is almost right, with garbage at the endbase64_decode dropped - or _Decode with JWT::urlsafeB64Decode
Class "Firebase\JWT\JWT" not foundThe autoloader isn't loadedrequire __DIR__ . '/vendor/autoload.php';

FAQ​

Why did php-jwt's README use base64_decode?​

Its README reads a token's header with base64_decode, and warns that reading a header that way "is vulnerable to attacks". Headers rarely contain - or _, so the sample works for typical headers; JWT::urlsafeB64Decode works for any piece.

Can I do this without php-jwt?​

Yes: swap the characters back and decode in strict mode, base64_decode(strtr($text, '-_', '+/'), true). Strict mode accepts missing padding and returns false for invalid characters.

Conclusion​

JWT::urlsafeB64Decode($text) decodes base64url correctly, padding or not. Plain base64_decode silently drops - and _, so don't use it on JWT pieces.

Additional Resources​