星期三, 10月 27, 2004

�]�\!!�i�H�Ψӳ]�wcache js...�ڲq��~

Cache Control with PHP: "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 the Content-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 objcet 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;"