Skip to content

Instantly share code, notes, and snippets.

@jamesliu96
Created August 14, 2019 16:53
Show Gist options
  • Save jamesliu96/51ef9828586f22e25c92bff28d1921a8 to your computer and use it in GitHub Desktop.
Save jamesliu96/51ef9828586f22e25c92bff28d1921a8 to your computer and use it in GitHub Desktop.
<?php
function cache(string $content) {
$filemtime = filemtime(__FILE__);
$length = strlen($content);
$etag = '"'.dechex($filemtime).'-'.dechex($length).'"';
$modified = gmdate("D, d M Y H:i:s", $filemtime)." GMT";
if ($_SERVER['HTTP_IF_NONE_MATCH'] === $etag || $_SERVER['HTTP_IF_MODIFIED_SINCE'] === $modified) {
http_response_code(304);
exit;
}
header("Content-Length: $length");
header("ETag: $etag");
header("Last-Modified: $modified");
echo $content;
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment