【问题标题】:php cache-control setting not workingphp缓存控制设置不起作用
【发布时间】:2013-12-20 16:48:27
【问题描述】:

我的标题中有以下功能

function header_alter($file)
{
    $timestamp=filemtime($file);
    $tsstring = gmdate('D, d M Y H:i:s ', $timestamp) . 'GMT';
    $etag = $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))
    {
        $arr[] = 'HTTP/1.1 304 Not Modified';       
    }
    else
    {
        $arr[] = "Last-Modified: $tsstring";
        $arr[] = "ETag: \"{$etag}\"";
    }
    $arr[] = "Cache-Control: max-age=3600";
    $arr[] = 'Expires: ' . date('D, d M Y H:i:s', time() + (3600)) . ' GMT';
    return $arr;
}

但是当我使用下面的代码查看标题时

foreach (getallheaders() as $name => $value) {
    echo "$name: $value\n";
}

Host: localhost
Connection: keep-alive
**Cache-Control: max-age=0**
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
If-None-Match: "1387557104"
If-Modified-Since: Fri, 19 Dec 2013 16:31:44 GMT

缓存控件未更新。谢谢大家的帮助。

【问题讨论】:

  • getallheaders() 返回客户端为当前请求发送的标头。客户端无法告诉服务器缓存控制设置 - 这仅适用于服务器->客户端响应。您需要使用客户端工具(例如 firebug、开发者控制台、等等等等)查看服务器发送的标头。
  • 我正在使用 chrome,我该怎么做?
  • ctrl-shift-i 打开 chrome 开发工具。点击网络标签,点击与您的脚本网址对应的请求,然后点击标题标签

标签: php http-headers cache-control


【解决方案1】:

我们可以使用 Cache-Control: max-age=... 通知浏览器组件在定义的时间段内不会更改。这样,如果浏览器的缓存中已经有组件,我们就可以避免不必要的进一步请求,因此准备缓存的页面视图将执行得更快。 现代浏览器即使在没有任何缓存控制标头的情况下也可以使用一些启发式方法缓存静态文件,但如果我们隐式定义缓存标头,它们会更有效。

对于 Apache2,您可以使用 mod_expires 启用 max-age:

ExpiresActive On
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"

对于 Lighttpd,有 mod_expire 模块。
在 server.modules 部分启用它:

server.modules = (
...
"mod_expire",
...
)

然后为包含静态文件的目录添加以下指令:

$HTTP["url"] =~ "^/images/" {
expire.url = ( "" => "access 30 days" )
}

可以使用 ngx_http_headers_module 启用 Nginx 服务器的 Max-age: 最大过期;

现在网络服务器发送静态文件的缓存头: 缓存控制:max-age=2592000

在设计更改的情况下,我们应该防止使用浏览器缓存中的过时内容。这可以通过将文件版本添加到文件名来完成: script.js -> script1.js -> script2.js -> ...等

Cache-control: max-age 在我们输出 HTML 时也很有用。想象一下由 PHP 生成的页面变化不那么频繁,每天一次甚至更长时间。但是浏览器仍然必须在每次页面浏览时下载 HTML。 我们可以通过在 PHP 中发送 max-age 值来改进它。

header('Cache-Control: max-age=28800');

【讨论】:

    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 2015-12-04
    • 2011-02-25
    • 1970-01-01
    • 2011-10-01
    • 2019-06-13
    相关资源
    最近更新 更多