HTTP_Header
[ class tree: HTTP_Header ] [ index: HTTP_Header ] [ all elements ]

Source for file Cache.php

Documentation is available at Cache.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 4.0                                                      |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 The PHP Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Wolfram Kriesing <wk@visionp.de>                            |
  16. // |                                                                      |
  17. // +----------------------------------------------------------------------+//
  18. // $Id: Cache.php,v 1.4 2003/12/09 18:31:42 cain Exp $
  19.  
  20. require_once 'HTTP/Header.php';
  21.  
  22. /**
  23. * This package provides methods to easier handle caching of HTTP pages.
  24. * That means that the pages can be cached at the client (User agent or browser)
  25. * and your application only needs to send "hey client you already have the pages".
  26. * Which is dont by sending the HTTP-Status 304 ('Not Modified'), so that your
  27. * application load can be reduced and the net traffic too, since you only need
  28. * to send the complete page once. This is really an advantage i.e. for generated
  29. * style sheet, or simply pages that do only change rarely.
  30. * I.e. when you dont want to send a client-side-cached page multiple times
  31. * you can do this:
  32. * <code>
  33. * $httpCache = new HTTP_Header_Cache();
  34. * // if the page is cached, then we send a 304-Not modified header here and EXIT the code right here!
  35. * // if not this method sets all the headers so that the page gets cached
  36. * $httpCache->exitIfCached();
  37. *
  38. * ...do the work that renders the real page, that shall be cached by the client...
  39. * </code>
  40. *
  41. * Or when you know that the page shall only be cached for some time:
  42. * <code>
  43. * $httpCache = new HTTP_Header_Cache();
  44. * // check if the page the client has is older than 2 days
  45. * if ($httpCache->isOlderThan(2,'days')) {
  46. *     $httpCache->sendHeaders(); // make sure that the headers, that tell this page shall be cached get sent
  47. *     ...generate and send all the cacheable content to the client...
  48. * } else {
  49. *     $httpCache->exitIfCached();
  50. * }
  51. *
  52. * </code>
  53. *
  54. @package HTTP_Header
  55. @author Wolfram Kriesing <wolfram@kriesing.de>
  56. */
  57. {
  58.  
  59.     var $_caching = true;
  60.  
  61.     /**
  62.     *
  63.     *
  64.     *
  65.     *   @param  boolean     shall the
  66.     *   @param  integer     the number of seconds after which the page expires
  67.     *                        0 - is never
  68.     */
  69.     function HTTP_Header_Cache($caching=true)
  70.     {
  71.         $this->_caching $caching;
  72.         if ($this->_caching{
  73.             $this->setHeader('Pragma','cache');
  74.             $this->setHeader('Cache-Control','public');
  75.         }
  76.     }
  77.  
  78.     function getCacheStart()
  79.     {
  80.         return $this->dateToTimestamp($_SERVER['HTTP_IF_MODIFIED_SINCE']);
  81.     }
  82.  
  83.     /**
  84.     * You can call it like this:
  85.     * <code>
  86.     *   $httpCache->isOlderThan(1,'day');
  87.     *   $httpCache->isOlderThan(47,'days');
  88.     *
  89.     *   $httpCache->isOlderThan(1,'week');
  90.     *   $httpCache->isOlderThan(3,'weeks');
  91.     *
  92.     *   $httpCache->isOlderThan(1,'hour');
  93.     *   $httpCache->isOlderThan(5,'hours');
  94.     *
  95.     *   $httpCache->isOlderThan(1,'minute');
  96.     *   $httpCache->isOlderThan(15,'minutes');
  97.     *
  98.     *   $httpCache->isOlderThan(1,'second');
  99.     *   $httpCache->isOlderThan(15);    // is the same as isOlderThan(15,'seconds')
  100.     * </code>
  101.     *
  102.     * @param integer the number of units, if no second paramter given it means seconds
  103.     * @param string the unit,
  104.     *   can be one of: 'week', 'weeks', 'day', 'days', 'hour', 'hours', 'minute', 'minutes', 'second'
  105.     * @return boolean true if it is older than what the parameters say
  106.     */
  107.     function isOlderThan($time=0$unit='')
  108.     {
  109.         switch (strtolower($unit)) {
  110.             case 'week':
  111.             case 'weeks':   $time $time*7;
  112.             case 'day':
  113.             case 'days':    $time $time*24;
  114.             case 'hour':
  115.             case 'hours':   $time $time*60;
  116.             case 'minute':
  117.             case 'minutes'$time $time*60;
  118.             case 'second':  break;
  119.         }
  120.         if ($time && $this->getCacheStart()+$time<time()) {
  121.             return true;
  122.         }
  123.         return false;
  124.     }
  125.  
  126.     /**
  127.     *   optionally you can pass an additional condition via parameter
  128.     *   which is simply checked for true
  129.     *   it would be the same as
  130.     *       $this->isCached() && $condition  ===  $this->isCached($condition)
  131.     *   it is suggested to use the parameter since the handling for sending the
  132.     *   'last-modified' is included in this method here
  133.     *
  134.     *   @author     Wolfram Kriesing <wolfram@kriesing.de>
  135.     *   @param      boolean 
  136.     */
  137.     function isCached$condition=true )
  138.     {
  139.         if isset($_SERVER['HTTP_IF_MODIFIED_SINCE']&& $condition==true {
  140.             return true;
  141.         }
  142.  
  143.         // if the file is not cached and 'caching' is on
  144.         // then it shall be cached, and therefore we need to send
  145.         // the 'last-modified' header in order to get a 'if-modified-since'
  146.         // next time, so that we can answer with a 304 in case it is still cached
  147.         if $this->_caching {
  148.             $this->setHeader'Last-Modified' );
  149.         }
  150.         return false;
  151.     }
  152.  
  153.  
  154.  
  155.     /**
  156.     *   this method exits the script if the page is
  157.     *   still cached by the user agent, the condition
  158.     *   if given will be AND-ed to the 'isCached' call
  159.     *   Since it returns false in case the page is not cached you can also use it in an
  160.     *   if. for example:
  161.     *       if (!$httpCache->exitIfCached()) {
  162.     *           do stuff here in case it is not cached
  163.     *       }
  164.     *
  165.     *   @param  boolean     will be AND-ed to the 'isCached' call
  166.     */
  167.     function exitIfCached$condition=true )
  168.     {
  169.         if ($this->isCached($condition)) {
  170.             $this->sendHeaders();
  171.             $this->sendStatusCode(304);
  172.             exit;
  173.         }
  174.         return false;
  175.     }
  176.  
  177.  
  178.  
  179.  
  180. /*  ???? does that make sense??? 'must-revalidate' is one of many possible values for Cache-Control
  181.     we could check HTTP-compilance here, see   http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
  182.     function mustRevalidate( $yes=true )
  183.     {
  184.         $this->setHeader('Cache-Control','must-revalidate');
  185.     }
  186. */
  187. }
  188. ?>

Documentation generated on Mon, 11 Mar 2019 10:17:27 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.