Without caching the page yourself (or at least its Etag), you cannot use 304. A complete caching algorithm is somewhat beyond the scope, but the general idea:
<?php function getUrlEtag($url){ //some logic to get an etag, possibly stored in memcached / database / file etc. } function setUrlEtag($url,$etag){ //some logic to get an etag, possibly stored in memcached / database / file etc. } function getPageCache($url,$etag=''){ //[optional]some logic to get the page from cache instead, possibly not even using etag } function setPageCache($url,$content,$etag=''){ //[optional]some logic to save the page to cache, possibly not even using etag } ob_start(); $etag = getUrlEtag($_SERVER['REQUEST_URI']); if(isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { header("HTTP/1.1 304 Not Modified"); exit; } if(($content=getPageCache($_SERVER['REQUEST_URI'],$etag))!==false){ echo $content; exit; } ?> //the actual page <?php $content = ob_get_clean(); setUrlEtag($_SERVER['REQUEST_URI'],$etag=md5($url.$content)); function setPageCache($_SERVER['REQUEST_URI'],$content,$etag); header("Etag: $etag"); echo $content; ?>
All common errors: you can not display cache pages for registered users, caching of partial content may be more desirable, you yourself are responsible for preventing obsolete content in the cache (possibly using triggers in the backend or database for modification or just playing with logic getUrlEtag ) etc. etc.
You can also play with HTTP_IF_MODIFIED_SINCE if it is easier to control.
source share