#native_company# #native_desc#
#native_cta#

Making PHP Applications Cache-Friendly Page 3

By Klaus A. Brunner
on November 16, 2001

Making PHP Applications Cache-Friendly

The basic approach I am using here is to touch a zero-length file
whenever the database is updated. The file’s modification time will
then serve as the Last-Modified date. This is very simple to
implement, but rather primitive as it does not differentiate
between forums: if something is posted in forum X, all
forums in the same database are considered “updated”. That’s
clearly not very effective when you have lots of posting activity
in more than one or two forums.
A finer-grained scheme — perhaps
down to tracking individual threads — would be more appropriate in
that case. Additionally, there are some issues with Phorum’s use
of cookies to flag unread messages; the workaround used here is
to allow a full reload periodically.
Please note that the following code is not release quality.
It works, but it’s rather simplistic and should be considered a
“proof of concept”. I simply patched Phorum
3.2.11 code where it seemed necessary to get quick results. My
changes are in bold print.

common.php:

<?php

  if ( !defined"_COMMON_PHP" ) ){

    
define("_COMMON_PHP");

 

  
// These variables may be altered as needed:

    $modification_file '/var/tmp/.phorum-update';

  // table name that Phorum uses to access meta-information on forums.

   

?>



include/header.php:

<?php

   global $modification_file;

   if(isset(
$modification_file) &amp;&amp; ($mtime filemtime($modification_file))) {

      
// to be on the safe side, refresh last modification time

      //  if it's "too old", forcing a reload from time to time.

      //   this is probably not necessary for most applications

      
if((mktime() - $mtime) &gt3600) {

         
touch($modification_file);

         
$mtime mktime();

      }

      $gmt_mtime gmdate('D, d M Y H:i:s'$mtime) . ' GMT';

      // we assume that the client generates proper RFC 822/1123 dates

      //   (should work for all modern browsers and proxy caches)

      
if ($HTTP_IF_MODIFIED_SINCE == $gmt_mtime) {

            
header("HTTP/1.1 304 Not Modified");

            exit;

      }

      $lastmod_header "Last-Modified: " $gmt_mtime;

      
Header($lastmod_header);

   }

   // your header modifications go here...

?>