Skip to content

Instantly share code, notes, and snippets.

@briandemant
Last active December 7, 2015 19:37
Show Gist options
  • Save briandemant/4d14fb4636b03a70adad to your computer and use it in GitHub Desktop.
Save briandemant/4d14fb4636b03a70adad to your computer and use it in GitHub Desktop.
php caching headers
function ifNoneMatch($etag, $expire = 60, $cacheAt = "public",$mime_type = 'application/json') {
header("Content-Type: $mime_type");
header("Cache-Control: max-age=$expire, $cacheAt=true");
header('Expires:' . gmdate('D, d M Y H:i:s T', strtotime("+$expire seconds", $timestamp)));
header('Etag:' . $etag);
// exit if not modified
if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
header("HTTP/1.1 304 Not Modified");
exit;
}
}
function ifModifiedSince($timestamp, $expire = 60, $cacheAt = "public",$mime_type = 'application/json') {
header("Content-Type: $mime_type");
header("Cache-Control: max-age=$expire, $cacheAt=true");
header('Expires:' . gmdate('D, d M Y H:i:s T', strtotime("+$expire seconds", $timestamp)));
header('Last-Modified:' . gmdate('D, d M Y H:i:s T', $timestamp));
// exit if not modified
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $timestamp) {
header("HTTP/1.1 304 Not Modified");
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment