如何在PHP中使用HTTP缓存头

时间:2009-12-28 21:43:37

标签: php http caching

我有一个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");

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

什么是“必须”有标题要发送以帮助缓存?

7 个答案:

答案 0 :(得分:45)

您可能希望使用private_no_expire代替private,但为您知道不会更改的内容设置较长的过期时间,并确保您处理if-modified-since和{{1}请求类似于Emil的帖子。

if-none-match

其中$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}\""); } 可以是基于内容或用户ID,语言和时间戳的校验和,例如

$etag

答案 1 :(得分:12)

您必须拥有Expires标头。从技术上讲,还有其他解决方案,但Expires标头实际上是最好的,因为它告诉浏览器在到期日期和时间之前不重新检查页面,只是从缓存中提供内容。它真的很棒!

在浏览器的请求中检查If-Modified-Since标头也很有用。当浏览器“不确定”时,如果其缓存中的内容仍然是正确的版本,则会发送此标头。如果您的页面从那时起未被修改,则只需发回HTTP 304代码(未修改)。这是一个发送304代码十分钟的例子:

<?php
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  if(strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) < time() - 600) {
    header('HTTP/1.1 304 Not Modified');
    exit;
  }
}
?>

您可以在代码中尽早进行此检查以节省服务器资源。

答案 2 :(得分:8)

<?php
header("Expires: Sat, 26 Jul 2020 05:00:00 GMT"); // Date in the future
?>

为缓存页面设置过期日期是在客户端缓存它的一种有用方法。

答案 3 :(得分:7)

选择 - 或全部使用! : - )

header('Expires: Thu, 01-Jan-70 00:00:01 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');

答案 4 :(得分:7)

这是一个为您执行http缓存的小类。它有一个名为'Init'的静态函数,需要2个参数,上次修改页面(或浏览器请求的任何其他文件)的日期的时间戳,以及此页面可以保存的最大年龄(以秒为单位)通过浏览器缓存。

class HttpCache 
{
    public static function Init($lastModifiedTimestamp, $maxAge)
    {
        if (self::IsModifiedSince($lastModifiedTimestamp))
        {
            self::SetLastModifiedHeader($lastModifiedTimestamp, $maxAge);
        }
        else 
        {
            self::SetNotModifiedHeader($maxAge);
        }
    }

    private static function IsModifiedSince($lastModifiedTimestamp)
    {
        $allHeaders = getallheaders();

        if (array_key_exists("If-Modified-Since", $allHeaders))
        {
            $gmtSinceDate = $allHeaders["If-Modified-Since"];
            $sinceTimestamp = strtotime($gmtSinceDate);

            // Can the browser get it from the cache?
            if ($sinceTimestamp != false && $lastModifiedTimestamp <= $sinceTimestamp)
            {
                return false;
            }
        }

        return true;
    }

    private static function SetNotModifiedHeader($maxAge)
    {
        // Set headers
        header("HTTP/1.1 304 Not Modified", true);
        header("Cache-Control: public, max-age=$maxAge", true);
        die();
    }

    private static function SetLastModifiedHeader($lastModifiedTimestamp, $maxAge)
    {
        // Fetching the last modified time of the XML file
        $date = gmdate("D, j M Y H:i:s", $lastModifiedTimestamp)." GMT";

        // Set headers
        header("HTTP/1.1 200 OK", true);
        header("Cache-Control: public, max-age=$maxAge", true);
        header("Last-Modified: $date", true);
    }
}

答案 5 :(得分:2)

我在来自Facebook Feed的服务器上进行JSON缓存,直到我发送flush和hid错误报告才有效。我知道这不是理想的代码,但需要快速修复。

error_reporting(0);
    $headers = apache_request_headers();
    //print_r($headers);
    $timestamp = time();
    $tsstring = gmdate('D, d M Y H:i:s ', $timestamp) . 'GMT';
    $etag = md5($timestamp);
    header("Last-Modified: $tsstring");
    header("ETag: \"{$etag}\"");
    header('Expires: Thu, 01-Jan-70 00:00:01 GMT');

    if(isset($headers['If-Modified-Since'])) {
            //echo 'set modified header';
            if(intval(time()) - intval(strtotime($headers['IF-MODIFIED-SINCE'])) < 300) {
              header('HTTP/1.1 304 Not Modified');
              exit();
            }
    }
    flush();
//JSON OP HERE

这非常有效。

答案 6 :(得分:0)

这是php缓存的最佳解决方案,只需在脚本顶部使用

$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");