简体   繁体   English

PHP 缓存控制似乎不起作用

[英]PHP cache control doesn't seem to work

Please take a look at my website: vynora请看我的网站: vynora

It's not finished.它还没有完成。 I have put a PHP header in the top of my HTML page:我在 HTML 页面的顶部放了一个 PHP header :

<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>
<?php
  header("Cache-Control: max-age=6000");
?>

When I go to pagespeed of Google it tells me that I should optimize my browser cache, please take a look: Google pagespeed当我 go 到 Google 的 pagespeed 时,它告诉我应该优化我的浏览器缓存,请看一下: Google pagespeed

But I already did using PHP.但我已经使用了 PHP。 So how is this possible?那么这怎么可能呢?

Problem not in this page and not in PHP scripts.问题不在此页面中,也不在 PHP 脚本中。 See Google's suggestions:查看谷歌的建议:

The following cacheable resources have a short freshness lifetime.以下可缓存资源具有较短的新鲜生命周期。 Specify an expiration at least one week in the future for the following resources:为以下资源指定至少一周后的到期时间:

It means, you should cache your static files.这意味着,您应该缓存static文件。
As I can see, you use Apache.如我所见,您使用的是 Apache。 In this case you can use mod_expires在这种情况下,您可以使用mod_expires

For example, you can add into .htaccess file this lines:例如,您可以在.htaccess文件中添加以下行:

ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/html "access plus 1 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 86400 seconds"
ExpiresByType application/x-javascript "access plus 86400 seconds"

To cache page into users browser add theses headers:要将页面缓存到用户浏览器中,请添加这些标题:

header("Cache-Control: private, max-age=6000, pre-check=6000");
header("Pragma: private");
header("Expires: " . gmdate("D, d M Y H:i:s"). " GMT");

gZip:压缩包:

http://www.whatsmyip.org/http_compression/?url=aHR0cDovL3d3dy52eW5vcmEuY29tLw==

says its gzipped说它的压缩包

http://redbot.org/?uri=http%3A%2F%2Fwww.vynora.com%2F

says its gzipped说它的压缩包

This may not work because there is possible whitespace before header().这可能不起作用,因为 header() 之前可能有空格。 Try it like this:试试这样:

<?php 
    if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
        ob_start("ob_gzhandler"); 
    } else {
        ob_start();
    }
    header("Cache-Control: max-age=6000");
?>

You should set the expired header as well, because old browsers do not understand "max-age".您也应该设置过期的 header,因为旧浏览器不理解“max-age”。

Btw.: Your server is currently sending "max-age: 0".顺便说一句:您的服务器当前正在发送“max-age:0”。

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

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