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. // | PEAR :: HTTP :: Header :: Cache                                      |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Authors: Wolfram Kriesing <wk@visionp.de>                            |
  12. // |          Michael Wallner <mike@php.net>                              |
  13. // +----------------------------------------------------------------------+
  14. //
  15. // $Id: Cache.php,v 1.17 2004/08/05 13:08:41 mike Exp $
  16.  
  17. require_once 'HTTP/Header.php';
  18.  
  19. /**
  20.  * HTTP_Header_Cache
  21.  * 
  22.  * This package provides methods to easier handle caching of HTTP pages.  That
  23.  * means that the pages can be cached at the client (user agent or browser) and
  24.  * your application only needs to send "hey client you already have the pages".
  25.  * 
  26.  * Which is done by sending the HTTP-Status "304 Not Modified", so that your
  27.  * application load and the network traffic can be reduced, since you only need
  28.  * to send the complete page once.  This is really an advantage e.g. for
  29.  * generated style sheets, or simply pages that do only change rarely.
  30.  * 
  31.  * Usage:
  32.  * <code>
  33.  *  require_once 'HTTP/Header/Cache.php';
  34.  *  $httpCache = new HTTP_Header_Cache(4, 'weeks');
  35.  *  $httpCache->sendHeaders();
  36.  *  // your code goes here
  37.  * </code>
  38.  * 
  39.  * @package     HTTP_Header
  40.  * @category    HTTP
  41.  * @license     PHP License
  42.  * @access      public
  43.  * @version     $Revision: 1.17 $
  44.  */
  45. {
  46.     /**
  47.      * Constructor
  48.      * 
  49.      * Set the amount of time to cache.
  50.      * 
  51.      * @access  public
  52.      * @return  object  HTTP_Header_Cache 
  53.      * @param   int     $expires 
  54.      * @param   string  $unit 
  55.      */
  56.     function HTTP_Header_Cache($expires = 0$unit 'seconds')
  57.     {
  58.         parent::HTTP_Header();
  59.         $this->setHeader('Pragma''cache');
  60.         $this->setHeader('Last-Modified'$this->getCacheStart());
  61.         $this->setHeader('Cache-Control''public');
  62.         
  63.         if ($expires{
  64.             if (!$this->isOlderThan($expires$unit)) {
  65.                 $this->exitCached();
  66.             }
  67.             $this->setHeader('Last-Modified'time());
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * Get Cache Start
  73.      * 
  74.      * Returns the unix timestamp of the If-Modified-Since HTTP header or the
  75.      * current time if the header was not sent by the client.
  76.      * 
  77.      * @access  public
  78.      * @return  int     unix timestamp
  79.      */
  80.     function getCacheStart()
  81.     {
  82.         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  83.             return strtotime(array_shift(explode(';'$_SERVER['HTTP_IF_MODIFIED_SINCE'])));
  84.         }
  85.         return time();
  86.     }
  87.  
  88.     /**
  89.      * Is Older Than
  90.      * 
  91.      * You can call it like this:
  92.      * <code>
  93.      *  $httpCache->isOlderThan(1, 'day');
  94.      *  $httpCache->isOlderThan(47, 'days');
  95.      * 
  96.      *  $httpCache->isOlderThan(1, 'week');
  97.      *  $httpCache->isOlderThan(3, 'weeks');
  98.      * 
  99.      *  $httpCache->isOlderThan(1, 'hour');
  100.      *  $httpCache->isOlderThan(5, 'hours');
  101.      * 
  102.      *  $httpCache->isOlderThan(1, 'minute');
  103.      *  $httpCache->isOlderThan(15, 'minutes');
  104.      * 
  105.      *  $httpCache->isOlderThan(1, 'second');
  106.      *  $httpCache->isOlderThan(15);
  107.      * </code>
  108.      * 
  109.      * If you specify something greater than "weeks" as time untit, it just
  110.      * works approximatly, because a month is taken to consist of 4.3 weeks.
  111.      * 
  112.      * @access  public
  113.      * @return  bool    Returns true if requested page is older than specified.
  114.      * @param   int     $time The amount of time.
  115.      * @param   string  $unit The unit of the time amount - (year[s], month[s],
  116.      *                   week[s], day[s], hour[s], minute[s], second[s]).
  117.      */
  118.     function isOlderThan($time = 0$unit 'seconds')
  119.     {
  120.         if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  121.             return true;
  122.         }
  123.         if (!$time{
  124.             return false;
  125.         }
  126.         
  127.         switch (strtolower($unit))
  128.         {
  129.             case 'year':
  130.             case 'years':
  131.                 $time *= 12;
  132.             case 'month':
  133.             case 'months':
  134.                 $time *= 4.3;
  135.             case 'week':
  136.             case 'weeks':
  137.                 $time *= 7;
  138.             case 'day':
  139.             case 'days':
  140.                 $time *= 24;
  141.             case 'hour':
  142.             case 'hours':
  143.                 $time *= 60;
  144.             case 'minute':
  145.             case 'minutes':
  146.                 $time *= 60;
  147.         }
  148.         
  149.         return (time($this->getCacheStart()) $time;
  150.     }
  151.  
  152.     /**
  153.      * Is Cached
  154.      * 
  155.      * Check whether we can consider to be cached on the client side.
  156.      * 
  157.      * @access  public
  158.      * @return  bool    Whether the page/resource is considered to be cached.
  159.      * @param   int     $lastModified Unix timestamp of last modification.
  160.      */
  161.     function isCached($lastModified = 0)
  162.     {
  163.         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']&& !$lastModified{
  164.             return true;
  165.         }
  166.         if (!$seconds time($lastModified{
  167.             return false;
  168.         }
  169.         return !$this->isOlderThan($seconds);
  170.     }
  171.     
  172.     /**
  173.      * Exit If Cached
  174.      * 
  175.      * Exit with "HTTP 304 Not Modified" if we consider to be cached.
  176.      * 
  177.      * @access  public
  178.      * @return  void 
  179.      * @param   int     $lastModified Unix timestamp of last modification.
  180.      */
  181.     function exitIfCached($lastModified = 0)
  182.     {
  183.         if ($this->isCached($lastModified)) {
  184.             $this->exitCached();
  185.         }
  186.     }
  187.     
  188.     /**
  189.      * Exit Cached
  190.      * 
  191.      * Exit with "HTTP 304 Not Modified".
  192.      * 
  193.      * @access  public
  194.      * @return  void 
  195.      */
  196.     function exitCached()
  197.     {
  198.         $this->sendHeaders();
  199.         $this->sendStatusCode(304);
  200.         exit;
  201.     }
  202.     
  203.     /**
  204.      * Set Last Modified
  205.      * 
  206.      * @access  public
  207.      * @return  void 
  208.      * @param   int     $lastModified The unix timestamp of last modification.
  209.      */
  210.     function setLastModified($lastModified = null)
  211.     {
  212.         $this->setHeader('Last-Modified'$lastModified);
  213.     }
  214. }
  215. ?>

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