简体   繁体   English

PHP的缓存控制设置不起作用

[英]php cache-control setting not working

i have the following function in my header 我的标头中具有以下功能

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;
}

but when i use the below code to see headers 但是当我使用以下代码查看标题时

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

The cache control is not updated. 缓存控件未更新。 Thnx for the help guys. 谢谢帮忙。

We can use Cache-Control: max-age=… to inform browser that the component won't be changed for defined period. 我们可以使用Cache-Control:max-age =…通知浏览器该组件在定义的时间内不会更改。 This way we avoid unneeded further requests if browser already has the component in its cache and therefore primed-cache page views will be performed faster. 这样,如果浏览器的缓存中已经包含该组件,我们就可以避免不必要的进一步请求,因此可以更快地执行已准备好缓存的页面视图。 Modern browsers able to cache static files even without any cache control headers using some heuristic methods but they will do it more efficient if we define caching headers implicitly. 现代浏览器即使使用任何启发式方法也可以缓存静态文件,即使没有任何缓存控制标头,但如果我们隐式定义缓存头,它们将更加有效。

For Apache2 you can enable max-age using mod_expires: 对于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"

For Lighttpd there is mod_expire module. 对于Lighttpd,有mod_expire模块。
Enable it in server.modules section: 在server.modules部分中启用它:

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

Then add following directives for directories with static files: 然后为包含静态文件的目录添加以下指令:

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

Max-age for Nginx server can be enabled using ngx_http_headers_module: expires max; 可以使用ngx_http_headers_module启用Nginx服务器的最大寿命。

Now web server sends the caching header for static files: Cache-Control: max-age=2592000 现在,Web服务器发送静态文件的缓存头:Cache-Control:max-age = 2592000

In case of design change we should prevent using outdated content that browsers have in their caches. 在设计变更的情况下,我们应避免使用浏览器缓存中过时的内容。 This can be done by adding file versions to filenames: script.js -> script1.js -> script2.js -> ... etc 这可以通过将文件版本添加到文件名来完成:script.js-> script1.js-> script2.js-> ...等

Cache-control: max-age can be useful also when we output HTML. 缓存控制:当我们输出HTML时,最大使用率也很有用。 Imagine pages generated by PHP that changed not so often, once per day or even longer. 想象一下由PHP生成的页面变化不那么频繁,每天一次甚至更长的时间。 But browsers still have to download HTML every page view. 但是浏览器仍然必须在每个页面视图中下载HTML。 We can improve it by sending max-age value in PHP. 我们可以通过在PHP中发送max-age值来改进它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM