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
| Status | Meaning |
|---|---|
HIT | Served from Cloudflare cache |
MISS | Not in cache, fetched from origin |
EXPIRED | Was cached but has expired, refetched |
STALE | Served stale while revalidating |
DYNAMIC | Not eligible for caching (e.g., POST requests, dynamic content) |
BYPASS | Cache 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=HIThttps://httpbin.org/response-headers?CF-Cache-Status=MISShttps://httpbin.org/response-headers?CF-Cache-Status=EXPIRED
More PHP Snippets
Array Difference
Find elements in the first array that are not present in the second array.
Array Frequencies
Count how many times each value appears in an array and return a frequency map.
Array Head
Get the first n elements of an array.
Array Intersection
Find common elements that exist in both arrays.
Array Tail
Get the last n elements of an array.
Array Union
Combine two arrays and remove duplicates to produce a union.