Enable caching of response

3 years ago

NoNoNo
Member | 3
+
0
-

Enable caching of response by issuing an ETag.

Example code:

<?php

header('Cache-Control: must-revalidate, max-age=0', true);
$sETag = md5($sResponseBody);
header("ETag: \"{$sETag}\"", true);
if ( isset($_SERVER['HTTP_IF_NONE_MATCH']) && stripos($_SERVER['HTTP_IF_NONE_MATCH'], $sETag) !== false ) {
  header('HTTP/1.1 304 Not Modified');
  exit;
}

// Cache-Control:
//   http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4
// ETag:
//   http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.3
// stripos, because HTTP_IF_NONE_MATCH may contain multiple, comma separated ETags:
//   http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26

?>