1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * Controls headers that effect client caching of pages
5 * $Id: expires.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
15 * Sets the amount of time before a page expires
17 * @param integer Seconds before the page expires
20 public static function set($seconds = 60)
22 if (expires
::check_headers())
24 $now = $expires = time();
26 // Set the expiration timestamp
30 header('Last-Modified: '.gmdate('D, d M Y H:i:s T', $now));
31 header('Expires: '.gmdate('D, d M Y H:i:s T', $expires));
32 header('Cache-Control: max-age='.$seconds);
41 * Checks to see if a page should be updated or send Not Modified status
43 * @param integer Seconds added to the modified time received to calculate what should be sent
44 * @return bool FALSE when the request needs to be updated
46 public static function check($seconds = 60)
48 if ( ! empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) AND expires
::check_headers())
50 if (($strpos = strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], ';')) !== FALSE)
52 // IE6 and perhaps other IE versions send length too, compensate here
53 $mod_time = substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, $strpos);
57 $mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
60 $mod_time = strtotime($mod_time);
61 $mod_time_diff = $mod_time +
$seconds - time();
63 if ($mod_time_diff > 0)
66 header('Last-Modified: '.gmdate('D, d M Y H:i:s T', $mod_time));
67 header('Expires: '.gmdate('D, d M Y H:i:s T', time() +
$mod_time_diff));
68 header('Cache-Control: max-age='.$mod_time_diff);
69 header('Status: 304 Not Modified', TRUE, 304);
72 Event
::add('system.display', array('expires', 'prevent_output'));
74 // Exit to prevent other output
83 * Check headers already created to not step on download or Img_lib's feet
87 public static function check_headers()
89 foreach (headers_list() as $header)
91 if (stripos($header, 'Last-Modified:') === 0 OR stripos($header, 'Expires:') === 0)
101 * Prevent any output from being displayed. Executed during system.display.
105 public static function prevent_output()
107 Kohana
::$output = '';