PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » PHP » Intelligent PHP Cache Control

Intelligent PHP Cache Control

This code snippet checks if a page has been modified since it was last displayed. If so, it sends a "304 not modified" header and exits, otherwise the content is rendered. Prepend this snippet on top of every PHP file you want to apply this intelligent caching-mechanism. Especially useful if you (have to) serve static content via php and want it to be cached like ordinary HTML or CSS.

<?php
//get the last-modified-date of this very file
$lastModified=filemtime(__FILE__);
//get a unique hash of this file (etag)
$etagFile = md5_file(__FILE__);
//get the HTTP_IF_MODIFIED_SINCE header if set
$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
//get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
$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;
}

//your normal code
echo "This page was last modified: ".date("d.m.Y H:i:s",time());

?>

Subscribe to The Thread

  1. Daniel

    Hi,
    does this code also work if i embed it in the header.php which is included in every file that could change?

    Thanks in advance!

  2. Can you please leave a short step-by-step tut on what variables need to be replaced and how to implement?

    Much thanks!

  3. Line with ETag should be:

    header("ETag: \"$etagFile\"");

  4. i’ve added your code to the very top of my pages and tested my pages at http://web-sniffer.net

    they keep coming back with the Status: HTTP/1.1 200 OK no matter how many times i have web-sniffer.net revisit the page, without me touching my files on the server.

    if your code was working in my page, i would see 200 OK on the first hit but the subsequent visits should say 304 (not modified), correct?

    when i change the $ifModifiedSince= variable in your code from false to true, i get the 304 (not modified) every time, which does not seem correct since the first visit should return 200 OK, correct?

    thank you!

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~