Check Cloudflare Cache Status

Check if a resource is served from Cloudflare's cache by inspecting the CF-Cache-Status header.

Code

General
$headers = get_headers($url, true);
return $headers['Cf-Cache-Status'] ?? $headers['CF-Cache-Status'] ?? 'No CF-Cache-Status header';

Parameters

URL to check

Server

CF-Cache-Status Values

StatusMeaning
HITServed from Cloudflare cache
MISSNot in cache, fetched from origin
EXPIREDWas cached but has expired, refetched
STALEServed stale while revalidating
DYNAMICNot eligible for caching (e.g., POST requests, dynamic content)
BYPASSCache bypassed due to configuration

Real-World Usage

function checkCFCache(string $url): array {
    $headers = get_headers($url, true);
    $status = $headers['Cf-Cache-Status'] ?? $headers['CF-Cache-Status'] ?? null;
    $age = $headers['Age'] ?? null;

    return [
        'cached' => $status === 'HIT',
        'status' => $status,
        'age' => $age !== null ? (int)$age : null
    ];
}

Testing with httpbin

Use httpbin to simulate different cache statuses:

  • https://httpbin.org/response-headers?CF-Cache-Status=HIT
  • https://httpbin.org/response-headers?CF-Cache-Status=MISS
  • https://httpbin.org/response-headers?CF-Cache-Status=EXPIRED

More PHP Snippets