Last active
December 7, 2015 19:37
php caching headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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