php: howto control page caching header

Working with cached pages

Controlling how web pages are cached is basically done using 2 kind of headers: Expires and Cache-Control

Using the Expire header is really simple. It tells when the page the browser or the proxy downloaded should be fetched again from the web server. In order to use it in your CGI or PHP page, just after theContent-type, you can add the the expire header as shown below:

 // calc an offset of 24 hours
 $offset = 3600 * 24;
 // calc the string in GMT not localtime and add the offset
 $expire = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
 //output the HTTP header
 Header($expire);

The Cache-Control HTTP Headers is part of the HTTP 1.1 standard. Here you are an example:

 Cache-Control: max-age=3600, must-revalidate

It has a certain number of parameters that can be used:

  • max-age=seconds – the number of seconds from the time of the request you wish this object to be keep into the cache;
  • s-maxage=seconds – like max-age but it only applies to proxy;
  • public – tell to handle the content has cacheable even if it would normally be uncacheable, it is used for example for authenticated pages;
  • no-cache – force both proxy and browser to validate the document before to provide a cached copy;
  • must-revalidate – tell the browser to obey to any information you give them about a webpage;
  • proxy-revalidate – like must-revalidate but applies to proxy;

 

More Headers

But that’s not all, you have to consider other 2 headers: Content-Length and Last-Modified.

The Last-Modified is the easier, you just have to output a date in GMT for example:

 $gmt_mtime = gmdate('D, d M Y H:i:s', time() ) . ' GMT';
 header("Last-Modified: " . $gmt_mtime );

Content-Lenght is the harder because you don’t know how long is a php page before processing it. If you output a file, for example and image or a PDF, it is simple because before of starting the output you can read the filesize. With dynamic page you have to use the special OB library, see the example:

 ob_start();
 ... your php code ...
 ... your php code ...
 ... your php code ...
 header('Content-Length: ' . ob_get_length());
 ob_end_flush();

The advantage is that you can use: ob_start('ob_gzhandler'); to have automatically compressed pages in output for faster downloading.

 

Usefull Links

Finally if you want to check the cacheability of your pages there are some urls available:

 

Important Notes

Remember that page caching is possible only for HTTP and not HTTPS because browsers never cache the results of crypted navigation to disks. This is the reason behind many websites keeping the images, flash and stuffs onto an HTTP server while they still provide crypted content via HTTPS.

 

php: howto control page caching header, 5.0 out of 5 based on 4 ratings

Potrebbe interessarti anche:

Iscriviti alla newsletter

  • HowTo e Tutorial su Linux e Ubuntu
  • Wordpress, Perl & Webmin
  • VoIP, Asterisk, SIP

Cosa stai aspettando?

  • http://www.badpenguin.org Antonio Gallo

    As Alon Wrk stated this article is about client caching. If you’re looking for server caching, i.e. like a temporary copy of the page stored on filesystem then there are a lot of PHP frameworks and templates system that offer that feature (chaing whole page) today. For instance, Codeigniter, offer caching of whole page (view) just doing $this->output->cache($minutes);

  • http://www.badpenguin.org Antonio Gallo

    you’re welcome ;-)

  • http://www.neatstat.com Pummy Manku

    thanks for the information, before this i dunno about cache. site is loading fast now.. just add the HTTP Headers cache expires.but how much time is best as i added 1 year behind date?

    • http://www.badpenguin.org Antonio Gallo

      it really depends when your resourse change, if the expire date is 1 year from now then i would suggest to serve that content as a static file instead of using PHP.

      It also depends on wich kind of application you are developing:

      for a blog page i will stay on 1 month; for the homepage i will stay on 1 day if it changes frequently

      however the most important header is also the “last modification time”: this should match the real modification time stored in your DB

      for homepage i will set it to the last modification time of the most recently published article

      remember, if you use expire date and lasst modification time from your DB, then if you change your skin/theme/layout within PHP it will be no reflected by the browser :) you’ll have to clean your browser cache;
      so turn this off on development environment

  • Pingback: Dreamhost上的GZIP & Cache @阅微堂

  • Pingback: Bad Penguin: Linux, PHP, Associazioni e altro

  • Pingback: Generating CSS Files Dynamically Using PHP Scripts? | Click & Find Answer !