OntoSys  

PHP Cache Control

This note describes a scheme for allowing PHP pages to be cached by a browser.

Example files

cache_check.inc -- Logic to support caching of dynamic pages

<?php
$if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE);

$mtime = filemtime($SCRIPT_FILENAME);
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';

if ($if_modified_since == $gmdate_mod) {
    header("HTTP/1.0 304 Not Modified");
    exit;
}
header("Last-Modified: $gmdate_mod");
?>

The value of the HTTP If-Modified-Since header (if any) is available in $HTTP_IF_MODIFIED_SINCE. We check the date value in that header against the modification date of the executed PHP script file itself. If they are the same, we send a 304 response and quit.

Otherwise, we send a Last-Modified header with the file's modification date.

cachable.php3 -- An example cacheable dynamically-generated file

<?php // -*- sgml-parent-document: ("dummy.html" "html" "body" ()) -*-
include 'cache_check.inc';

if (isset($touch))  touch($SCRIPT_FILENAME);
$gmdate_now = gmdate('D, d M Y H:i:s') . ' GMT';
$now = time();

print "
<table>
<tr><td>if_modified_since</td><td>$if_modified_since</td></tr>
<tr><td>gmdate_mod</td><td>$gmdate_mod</td></tr>
<tr><td>gmdate_now</td><td>$gmdate_now</td></tr>
</table>
<p>
<a href=\"$SCRIPT_NAME\">Link to self</a>.<br>
<a href=\"$SCRIPT_NAME?touch=y&time=$now\">Update source file</a>.<br>
<a href=\"$SCRIPT_NAME?time=$now\">Link to self with varying URL</a>.
";
?>

This dynamic page simply generates some output for testing purposes. Note that the gmdate_now value will not appear to change if the browser uses the file from its cache or if the server sends back a 304.

Try it yourself

Here is a link to cacheable.php3. Load that page in another window and try the following in Netscape.

Action Visable Response What happened
Network traffic Date displayed
Click "link to self" None No change No validation; browser reloaded page from cache
Click Reload Brief No change Browser validated page with server, but got 304 response and used cache
Shift-click Reload Normal Changes Browser requested page without sending If-Modified-Since header; server generated new page

You may see network traffic on all three tests if you have Netscape configured to validate "every time" in its cache preferences.

Discussion

This scheme works very well in conjunction with a PHP templating scheme. A templating utility class can implement the cache-control logic and hide it from the page instances.

This scheme assumes that the last-modified date of the PHP3 file itself determines the modification date of the generated page. That makes sense in templating schemes, but might not be appropriate if the page's code accesses other data that could change such as from a database.


OntoSys, Inc. - 38W242 Deerpath Rd - Batavia, IL 60510
phone +1.630.879.1312 - fax +1.630.879.1370
email info@ontosys.com - www.ontosys.com
Last modified: 1970-01-01 00:00 Z.
Copyright © 1999 - 2004 by Fred Yankowski.
All rights reserved.