Papermashup

Subscribe


Tweets


"RT @bethgordon: .@Tesco Please rethink archaic 'Approved by Mums'. Pretty sure it's not just mothers who make childcare decisions. https://…"

@ashleyford 2 days ago

"Is there ever a time that @DFS doesn't have a sale on?"

@ashleyford 6 days ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Papermashup

PHP Clever Caching

Temporarily storing recently used information. The content, which includes HTML pages, images, files and Web objects

AshleyAshley

This clever bit of code checks if a page has been modified since it was last displayed and if it hasn’t, it sends a “304 not modified” header and exits, otherwise the content is loads as normal. Add the code below to the top of each PHP page you want to apply it to. It’s especially useful if you serve static content via php and want it to be cached like ordinary HTML or CSS.



//get the last-modified-date of this very file
$lastModified=filemtime(__FILE__);
//get a hash of this file
$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
$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 header and exit
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==$lastModified || $etagHeader == $etagFile)
{
header("HTTP/1.1 304 Not Modified");
exit;
}

echo "Page last modified: ".date("d.m.Y H:i:s",time());

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 6
  • mjb4
    Posted on

    mjb4 mjb4

    Reply Author

    This would not work for a php file which is printing the current date.
    All cached clients would just see the date of the caching date.

    Wouldn’t it a bit more prudent to use the output buffers and then check for differences?


    • mjb4
      Posted on

      mjb4 mjb4

      Reply Author

      Or put otherwise this will just work for files you could execute once and safe the output as file.


  • Loi dich
    Posted on

    Loi dich Loi dich

    Reply Author

    Thanks for this nice infomation


  • Loi dich
    Posted on

    Loi dich Loi dich

    Reply Author

    Thanks for share !, i finding it


  • Ochab
    Posted on

    Ochab Ochab

    Reply Author

    I think it’s more useful to do this on the CSS and JS only, as HTML caching is not generally a problem. For example: http://cachebust.in/


  • Fabien
    Posted on

    Fabien Fabien

    Reply Author

    Why use both etag and last-modified ?
    Etag just seems overkill, it adds nothing but takes bandwidth (additionnal headers) and UC time (md5 computation).