demo.php

This is the source of the demo available from http://larve.net/2006/08/php-http-caching/

Here is an example output: http://larve.net/2006/08/php-http-caching/demo.php?expirein=2%20hours

00001 <?php
00002 require('HttpCaching.php');
00003 
00004 define("DEFAULT", "1 day");
00005 
00006 $howlong = $_GET['expirein'];
00007 
00008 $hc = new HttpCaching();
00009 $hc->ccMustRevalidate(true);
00010 $hc->setLastModifiedFromFile(__FILE__);
00011 
00012 if ($howlong == "") {
00013   $diag = 'No caching directives requested';
00014   $hc->freshFor(constant("DEFAULT"));
00015 } else {
00016   try {
00017     $d = $hc->freshFor($howlong);
00018     if ($d >= 0) {
00019       $diag = "Caching header set to cache for '$howlong', i.e. declared fresh until ".
00020         HttpCaching::formatDate(time() + $d);
00021     } else {
00022       $diag = "No expiry data sent back in HTTP headers";
00023     }
00024   } catch(Exception $e) {
00025     $diag = $e->getMessage();
00026     $hc->freshFor(constant("DEFAULT"));
00027   }
00028 }
00029 
00030 $hc->sendStatusAndHeaders(true);
00031 ?>
00032 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
00033 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
00034 <head>
00035 <title>HttpCaching demonstration</title>
00036 </head>
00037 <body>
00038 <p>
00039 <?php
00040 print $diag;
00041 ?>
00042 </p>
00043 <hr />
00044 <p>See <a href="./">HttpCaching home page</a> for details.</p>
00045 </body>
00046 </html>

PHP HTTP Caching
Hugo Haas