猿问

如何在PHP中使用HTTP缓存头

我有一个PHP 5.1.0网站(实际上是5.2.9,但它也必须在5.1.0+上运行)。


页面是动态生成的,但是许多页面大多数是静态的。静态是指内容不变,但是内容周围的“模板”会随着时间而改变。


我知道他们已经有几个缓存系统和PHP框架,但是我的主机没有安装APC或Memcached,并且我没有为该特定项目使用任何框架。


我希望页面被缓存(我认为默认情况下PHP“不允许”缓存)。到目前为止,我正在使用:


session_cache_limiter('private'); //Aim at 'public'

session_cache_expire(180);

header("Content-type: $documentMimeType; charset=$documentCharset");

header('Vary: Accept');

header("Content-language: $currentLanguage");

我读了许多教程,但找不到简单的东西(我知道缓存很复杂,但是我只需要一些基本的东西)。


什么是必须发送的标头才能帮助缓存?


斯蒂芬大帝
浏览 446回答 3
3回答

慕的地8271018

您可能要使用private_no_expire而不是private,但是为您知道不会更改的内容设置一个较长的到期时间,并确保您处理if-modified-since和if-none-match请求的内容类似于Emil的帖子。$tsstring = gmdate('D, d M Y H:i:s ', $timestamp) . 'GMT';$etag = $language . $timestamp;$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false;if ((($if_none_match && $if_none_match == $etag) || (!$if_none_match)) &&    ($if_modified_since && $if_modified_since == $tsstring)){    header('HTTP/1.1 304 Not Modified');    exit();}else{    header("Last-Modified: $tsstring");    header("ETag: \"{$etag}\"");}$etag根据内容或用户ID,语言和时间戳记的校验和可能在哪里,例如$etag = md5($language . $timestamp);

拉丁的传说

这是一个为您执行http缓存的小类。它有一个名为“ Init”的静态函数,它需要2个参数,该页面(或浏览器请求的任何其他文件)的最后修改日期的时间戳,以及该页面可以保留的最大期限(以秒为单位)。浏览器缓存。class HttpCache&nbsp;{&nbsp; &nbsp; public static function Init($lastModifiedTimestamp, $maxAge)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (self::IsModifiedSince($lastModifiedTimestamp))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self::SetLastModifiedHeader($lastModifiedTimestamp, $maxAge);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self::SetNotModifiedHeader($maxAge);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static function IsModifiedSince($lastModifiedTimestamp)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $allHeaders = getallheaders();&nbsp; &nbsp; &nbsp; &nbsp; if (array_key_exists("If-Modified-Since", $allHeaders))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $gmtSinceDate = $allHeaders["If-Modified-Since"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sinceTimestamp = strtotime($gmtSinceDate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Can the browser get it from the cache?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($sinceTimestamp != false && $lastModifiedTimestamp <= $sinceTimestamp)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; private static function SetNotModifiedHeader($maxAge)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Set headers&nbsp; &nbsp; &nbsp; &nbsp; header("HTTP/1.1 304 Not Modified", true);&nbsp; &nbsp; &nbsp; &nbsp; header("Cache-Control: public, max-age=$maxAge", true);&nbsp; &nbsp; &nbsp; &nbsp; die();&nbsp; &nbsp; }&nbsp; &nbsp; private static function SetLastModifiedHeader($lastModifiedTimestamp, $maxAge)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Fetching the last modified time of the XML file&nbsp; &nbsp; &nbsp; &nbsp; $date = gmdate("D, j M Y H:i:s", $lastModifiedTimestamp)." GMT";&nbsp; &nbsp; &nbsp; &nbsp; // Set headers&nbsp; &nbsp; &nbsp; &nbsp; header("HTTP/1.1 200 OK", true);&nbsp; &nbsp; &nbsp; &nbsp; header("Cache-Control: public, max-age=$maxAge", true);&nbsp; &nbsp; &nbsp; &nbsp; header("Last-Modified: $date", true);&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答