Youss
Youss

Reputation: 4232

PHP cache control doesn't seem to work

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:

<?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

But I already did using PHP. So how is this possible?

Upvotes: 3

Views: 5689

Answers (3)

user680786
user680786

Reputation:

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:

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"

Upvotes: 2

reeaal
reeaal

Reputation: 346

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".

Upvotes: 1

Lawrence Cherone
Lawrence Cherone

Reputation: 46650

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

Upvotes: 1

Related Questions