什么 ceejayoz 说 或者,您可以在应用程序的引导程序中添加一个缓存标头,一个您认为想要的多年的缓存。
您使用要在客户端缓存页面的小时数调用附加函数,如果有,请务必在 session_start 之后调用此函数,因为 session_start 会发出阻止缓存的标头。
function client_side_cache($hours)
{
//in the event a session start is used, I have to clean all the #$%# headers it sends to prevent caching
header('Cache-Control: ',true);
header("Pragma: ", true);
header("Expires: ", true);
//get the If-Modified-Since header in a unix time format
$headers = getallheaders();
if (isset($headers['If-Modified-Since']))
{
$modifiedSince = explode(';', $headers['If-Modified-Since']);
$modifiedSince = strtotime($modifiedSince[0]);
}
else
{
$modifiedSince = 0;
}
//calculate the Last-Modified timestamp
$current_time=time();
$last_modified=($current_time)/($hours*3600);
$last_modified=(int)$last_modified;
$last_modified=$last_modified*$hours*3600;
//check cache not expires
if ($last_modified <= $modifiedSince)
{
header('HTTP/1.1 304 Not Modified');
exit();
}
else //emit a new Last-Modified (either cache expired or page wasn'r cached
{
Header('Last-Modified: '.gmdate("D, d M Y H:i:s",$last_modified).' GMT ');
}
}