How to generate URL authentication tokens and expiry with PHP on BunnyCDN (Bunny Net) URLs.
This means that URL’s fetched that do not have a valid token and expiry will return a 403 HTTP response.
Using token authentication on URLs makes traffic need to go through your website rather than directly accessing the media.
Here is a PHP function created with details from the Bunny net blog post.
function URLTokenGenerate(string $path, string $zone_url, int $expires_seconds = 3600): string { $security_key = 'URL-TOKEN-AUTH-KEY-HERE'; $expires = (time() + $expires_seconds); $hash_base = $security_key . $path . $expires; $token = md5($hash_base, true); $token = base64_encode($token); $token = strtr($token, '+/', '-_'); $token = str_replace('=', '', $token); return "{$zone_url}{$path}?token={$token}&expires={$expires}"; }
Put in your token authentication key at $security_key
An example call:
$url = URLTokenGenerate('/images/banner.jpg', 'https://myweb.b-cdn.net', 300);
Will return similar to
https://myweb.b-cdn.net/images/banner.jpg?token=_AWA1k7ukqLqx2EyXUhrHA&expires=1627651686
This URL will be valid until 5 minutes (300 seconds) after creation.