Content · Web Development · PHP

Generating and Validating Signed URLs

Signed URLs carry an expiry and integrity signature so a server can detect tampering. Compare signatures in constant time and authorize the underlying resource; prevent replay if one-time access.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Signed URLs carry an expiry and integrity signature so a server can detect tampering.

What It Does


Signed URLs carry an expiry and integrity signature so a server can detect tampering. The following examples distinguish a normal use case, a boundary case and a practical application.

Requirements and Compatibility


PHP 8.x and a local test environment. Apply input validation, correct error handling and the requirements of any optional libraries.

The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.

Example 1: Basic Behavior


Understand the primary operation and its inputs.

<?php
$path = '/download/report/8'; $expires = time() + 300; $secret = 'demo-key';
$signature = hash_hmac('sha256', $path . '|' . $expires, $secret);
echo $signature;

Example 2: Another Case


Check failure behavior or a different execution path.

<?php
$path = '/reports/8'; $expiry = time() + 30; $secret = 'demo-key';
$signature = hash_hmac('sha256', $path . '|' . $expiry, $secret);
$allowed = $expiry >= time() && hash_equals(hash_hmac('sha256', $path . '|' . $expiry, $secret), $signature);
var_dump($allowed);

Practical Example: Putting It to Use


Apply the behavior in a small, controlled scenario.

<?php
$path = '/private/file'; $expiry = time() - 2; $secret = 'demo-key';
$sig = hash_hmac('sha256', $path . '|' . $expiry, $secret);
var_dump($expiry >= time() && hash_equals(hash_hmac('sha256', $path . '|' . $expiry, $secret), $sig));

Important Behavior and Edge Cases


Compare signatures in constant time and authorize the underlying resource; prevent replay if one-time access matters.

Comparison with Related Functions or Approaches


OptionWhat it means in practice
Generating and Validating Signed URLsSigned URLs carry an expiry and integrity signature so a server can detect tampering.
Related approachUse hash_hmac() with an application secret and include every security-relevant parameter.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the important boundary for Generating and Validating Signed URLsCompare signatures in constant time and authorize the underlying resource; prevent replay if one-time access matters.
Assuming a related mechanism has the same behaviorUse hash_hmac() with an application secret and include every security-relevant parameter.

Key Takeaways


  • Signed URLs carry an expiry and integrity signature so a server can detect tampering.
  • Compare signatures in constant time and authorize the underlying resource; prevent replay if one-time access matters.
  • Use hash_hmac() with an application secret and include every security-relevant parameter.

Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.

Share this resourceSend this page to someone who may find it useful.