Created
December 22, 2020 23:05
-
-
Save dejurin/bf800014af6e2ef023ed795fbde8bf80 to your computer and use it in GitHub Desktop.
How to use HTTP cache headers with PHP
This file contains 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
<?php | |
// https://stackoverflow.com/a/1973016/3360295 | |
$tsstring = gmdate('D, d M Y H:i:s ', $timestamp) . 'GMT'; | |
$etag = $language . $timestamp; | |
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false; | |
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false; | |
if ((($if_none_match && $if_none_match == $etag) || (!$if_none_match)) && | |
($if_modified_since && $if_modified_since == $tsstring)) | |
{ | |
header('HTTP/1.1 304 Not Modified'); | |
exit(); | |
} | |
else | |
{ | |
header("Last-Modified: $tsstring"); | |
header("ETag: \"{$etag}\""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment