Skip to content

Instantly share code, notes, and snippets.

@dejurin
Created December 22, 2020 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dejurin/bf800014af6e2ef023ed795fbde8bf80 to your computer and use it in GitHub Desktop.
Save dejurin/bf800014af6e2ef023ed795fbde8bf80 to your computer and use it in GitHub Desktop.
How to use HTTP cache headers with PHP
<?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