PHP cache control doesn't seem to work

前端 未结 3 2027
难免孤独
难免孤独 2021-01-14 19:22

Please take a look at my website:vynora

It\'s not finished. I have put a PHP header in the top of my HTML page:



        
相关标签:
3条回答
  • 2021-01-14 19:26

    This may not work because there is possible whitespace before 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".

    Btw.: Your server is currently sending "max-age: 0".

    0 讨论(0)
  • 2021-01-14 19:36

    Problem not in this page and not in PHP scripts. 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:

    • http://www.vynora.com/arrow.png (expiration not specified)
    • http://www.vynora.com/bing.png (expiration not specified)
    • http://www.vynora.com/dailymotion.png (expiration not specified)
    • http://www.vynora.com/dd_arrow.gif (expiration not specified)
    • http://www.vynora.com/deviantart.png (expiration not specified)
    • http://www.vynora.com/flickr.png (expiration not specified)
    • http://www.vynora.com/google.png (expiration not specified)
    • ...

    It means, you should cache your static files.
    As I can see, you use Apache. In this case you can use mod_expires

    For example, you can add into .htaccess file this lines:

    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"
    
    0 讨论(0)
  • 2021-01-14 19:52

    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

    0 讨论(0)
提交回复
热议问题