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

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