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.19 2004/08/05 15:48:20 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.19 $
  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''private, must-revalidate, max-age=0');
  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(';'
  84.                 $_SERVER['HTTP_IF_MODIFIED_SINCE'])));
  85.         }
  86.         return time();
  87.     }
  88.  
  89.     /**
  90.      * Is Older Than
  91.      * 
  92.      * You can call it like this:
  93.      * <code>
  94.      *  $httpCache->isOlderThan(1, 'day');
  95.      *  $httpCache->isOlderThan(47, 'days');
  96.      * 
  97.      *  $httpCache->isOlderThan(1, 'week');
  98.      *  $httpCache->isOlderThan(3, 'weeks');
  99.      * 
  100.      *  $httpCache->isOlderThan(1, 'hour');
  101.      *  $httpCache->isOlderThan(5, 'hours');
  102.      * 
  103.      *  $httpCache->isOlderThan(1, 'minute');
  104.      *  $httpCache->isOlderThan(15, 'minutes');
  105.      * 
  106.      *  $httpCache->isOlderThan(1, 'second');
  107.      *  $httpCache->isOlderThan(15);
  108.      * </code>
  109.      * 
  110.      * If you specify something greater than "weeks" as time untit, it just
  111.      * works approximatly, because a month is taken to consist of 4.3 weeks.
  112.      * 
  113.      * @access  public
  114.      * @return  bool    Returns true if requested page is older than specified.
  115.      * @param   int     $time The amount of time.
  116.      * @param   string  $unit The unit of the time amount - (year[s], month[s],
  117.      *                   week[s], day[s], hour[s], minute[s], second[s]).
  118.      */
  119.     function isOlderThan($time = 0$unit 'seconds')
  120.     {
  121.         if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  122.             return true;
  123.         }
  124.         if (!$time{
  125.             return false;
  126.         }
  127.         
  128.         switch (strtolower($unit))
  129.         {
  130.             case 'year':
  131.             case 'years':
  132.                 $time *= 12;
  133.             case 'month':
  134.             case 'months':
  135.                 $time *= 4.3;
  136.             case 'week':
  137.             case 'weeks':
  138.                 $time *= 7;
  139.             case 'day':
  140.             case 'days':
  141.                 $time *= 24;
  142.             case 'hour':
  143.             case 'hours':
  144.                 $time *= 60;
  145.             case 'minute':
  146.             case 'minutes':
  147.                 $time *= 60;
  148.         }
  149.         
  150.         return (time($this->getCacheStart()) $time;
  151.     }
  152.  
  153.     /**
  154.      * Is Cached
  155.      * 
  156.      * Check whether we can consider to be cached on the client side.
  157.      * 
  158.      * @access  public
  159.      * @return  bool    Whether the page/resource is considered to be cached.
  160.      * @param   int     $lastModified Unix timestamp of last modification.
  161.      */
  162.     function isCached($lastModified = 0)
  163.     {
  164.         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']&& !$lastModified{
  165.             return true;
  166.         }
  167.         if (!$seconds time($lastModified{
  168.             return false;
  169.         }
  170.         return !$this->isOlderThan($seconds);
  171.     }
  172.     
  173.     /**
  174.      * Exit If Cached
  175.      * 
  176.      * Exit with "HTTP 304 Not Modified" if we consider to be cached.
  177.      * 
  178.      * @access  public
  179.      * @return  void 
  180.      * @param   int     $lastModified Unix timestamp of last modification.
  181.      */
  182.     function exitIfCached($lastModified = 0)
  183.     {
  184.         if ($this->isCached($lastModified)) {
  185.             $this->exitCached();
  186.         }
  187.     }
  188.     
  189.     /**
  190.      * Exit Cached
  191.      * 
  192.      * Exit with "HTTP 304 Not Modified".
  193.      * 
  194.      * @access  public
  195.      * @return  void 
  196.      */
  197.     function exitCached()
  198.     {
  199.         $this->sendHeaders();
  200.         $this->sendStatusCode(304);
  201.         exit;
  202.     }
  203.     
  204.     /**
  205.      * Set Last Modified
  206.      * 
  207.      * @access  public
  208.      * @return  void 
  209.      * @param   int     $lastModified The unix timestamp of last modification.
  210.      */
  211.     function setLastModified($lastModified = null)
  212.     {
  213.         $this->setHeader('Last-Modified'$lastModified);
  214.     }
  215. }
  216. ?>

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