为了账号安全,请及时绑定邮箱和手机立即绑定

如何在PHP中使用HTTP缓存头

如何在PHP中使用HTTP缓存头

PHP
斯蒂芬大帝 2019-10-17 10:18:11
我有一个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");我读了许多教程,但找不到简单的东西(我知道缓存很复杂,但是我只需要一些基本的东西)。什么是必须发送的标头才能帮助缓存?
查看完整描述

3 回答

?
慕的地8271018

TA贡献1796条经验 获得超4个赞

您可能要使用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);


查看完整回答
反对 回复 2019-10-17
?
拉丁的传说

TA贡献1789条经验 获得超8个赞

这是一个为您执行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);

    }

}


查看完整回答
反对 回复 2019-10-17
  • 3 回答
  • 0 关注
  • 445 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信