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.10 2004/07/02 11:52:06 mike Exp $
  16.  
  17. require_once 'HTTP/Header.php';
  18.  
  19. /**
  20.  * HTTP_Header
  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.10 $
  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.         $this->setHeader('Pragma''cache');
  59.         $this->setHeader('Cache-Control''public');
  60.         $this->setHeader('Last-Modified'$this->getCacheStart());
  61.         
  62.         if ($expires && !$this->isOlderThan($expires$unit)) {
  63.             $this->exitIfCached();
  64.         }
  65.     }
  66.  
  67.     /**
  68.      * Get Cache Start
  69.      * 
  70.      * Returns the unix timestamp of the If-Modified-Since HTTP header or the
  71.      * current time if the header was not sent by the client.
  72.      * 
  73.      * @access  public
  74.      * @return  int     unix timestamp
  75.      */
  76.     function getCacheStart()
  77.     {
  78.         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  79.             return strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
  80.         }
  81.         return time();
  82.     }
  83.  
  84.     /**
  85.      * Is Older Than
  86.      * 
  87.      * You can call it like this:
  88.      * <code>
  89.      *  $httpCache->isOlderThan(1, 'day');
  90.      *  $httpCache->isOlderThan(47, 'days');
  91.      * 
  92.      *  $httpCache->isOlderThan(1, 'week');
  93.      *  $httpCache->isOlderThan(3, 'weeks');
  94.      * 
  95.      *  $httpCache->isOlderThan(1, 'hour');
  96.      *  $httpCache->isOlderThan(5, 'hours');
  97.      * 
  98.      *  $httpCache->isOlderThan(1, 'minute');
  99.      *  $httpCache->isOlderThan(15, 'minutes');
  100.      * 
  101.      *  $httpCache->isOlderThan(1, 'second');
  102.      *  $httpCache->isOlderThan(15);
  103.      * </code>
  104.      * 
  105.      * If you specify something greater than "weeks" as time untit, it just
  106.      * works approximatly, because a month is taken to consist of 4.3 weeks.
  107.      * 
  108.      * @access  public
  109.      * @return  bool    Returns true if requested page is older than specified.
  110.      * @param   int     $time The amount of time.
  111.      * @param   string  $unit The unit of the time amount - (year[s], month[s],
  112.      *                   week[s], day[s], hour[s], minute[s], second[s]).
  113.      */
  114.     function isOlderThan($time = 0$unit 'seconds')
  115.     {
  116.         static $cacheStart;
  117.         
  118.         if (!$time{
  119.             return false;
  120.         }
  121.         
  122.         if (!isset($cacheStart)) {
  123.             $cacheStart $this->getCacheStart();
  124.         }
  125.         
  126.         switch (strtolower($unit))
  127.         {
  128.             case 'year':
  129.             case 'years':
  130.                 $time *= 12;
  131.             case 'month':
  132.             case 'months':
  133.                 $time *= 4.3;
  134.             case 'week':
  135.             case 'weeks':
  136.                 $time *= 7;
  137.             case 'day':
  138.             case 'days':
  139.                 $time *= 24;
  140.             case 'hour':
  141.             case 'hours':
  142.                 $time *= 60;
  143.             case 'minute':
  144.             case 'minutes':
  145.                 $time *= 60;
  146.             default:
  147.                 $time += $cacheStart;
  148.         }
  149.         
  150.         return $time time();
  151.     }
  152.  
  153.     /**
  154.      * Is Cached
  155.      * 
  156.      * @access  public
  157.      * @return  bool    Whether the page/resource is considered to be cached.
  158.      * @param   int     $lastModified Unix timestamp of last modification.
  159.      */
  160.     function isCached($lastModified = 0)
  161.     {
  162.         if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  163.             return false;
  164.         }
  165.         if (!$lastModified{
  166.             return true;
  167.         }
  168.         return $lastModified $this->getCacheStart();
  169.     }
  170.     
  171.     /**
  172.      * Exit If Cached
  173.      * 
  174.      * @access  public
  175.      * @return  void 
  176.      */
  177.     function exitIfCached()
  178.     {
  179.         if ($this->isCached()) {
  180.             $this->sendHeaders();
  181.             $this->sendStatusCode(304);
  182.             exit;
  183.         }
  184.     }
  185.     
  186.     /**
  187.      * Set Last Modified
  188.      * 
  189.      * @access  public
  190.      * @return  void 
  191.      * @param   int     $lastModified The unix timestamp of last modification.
  192.      */
  193.     function setLastModified($lastModified = null)
  194.     {
  195.         $this->setHeader('Last-Modified'$lastModified);
  196.     }
  197. }
  198. ?>

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