Reading:
Cache Control: 304 Not Modified

Cache Control: 304 Not Modified

Metamug
Cache Control: 304 Not Modified

Cache Control header

Set the cache control header to public

Cache-Control: public

Last Modified header

Get the HTTP_IF_MODIFIED_SINCE header if set

$lastModified=filemtime($filePath);
$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
//set last-modified header
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");

Setup ETag

Get a unique hash of this file (etag). Then Get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)

$etagFile = md5_file($filePath);
$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
header("Etag: $etagFile");

Send 304 Not Modified Status Code

//check if page has changed. If not, send 304 and exit
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==$lastModified || $etagHeader == $etagFile)
{
       header("HTTP/1.1 304 Not Modified");
       exit;
}

PHP Script for Cache Control

$lastModified=filemtime($filePath);
$etagFile = md5_file($filePath);

$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);

//set last-modified header
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
//set etag-header
header("Etag: $etagFile");
//make sure caching is turned on
header('Cache-Control: public');

//check if page has changed. If not, send 304 and exit
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==$lastModified || $etagHeader == $etagFile)
{
       header("HTTP/1.1 304 Not Modified");
       exit;
}


Comments

Post a comment
Icon For Arrow-up