HTML and CSS Reference
Proxy servers such as AOL's use caches to improve local performance on their network. If 10,000 people
request the New York Times home page between 6:00 a.m. and 7:00 a.m. every day, AOL has to load it from
the New York Times only once. Then it can serve its local copy to all of its users. Individually, users often return
to the same pages more than once. A browser that has gone to a page before can save time by reloading it
from a local cache rather than downloading it from the network again. This saves everyone's bandwidth,
reduces latency, and is an all around good thing. It is one reason the Web scales as well as it does.
Usually browsers, proxy servers, and other software cache documents only if the server indicates that's OK. The
server does that by setting one or more of the following HTTP headers:
They do not cache documents if
The document was loaded via https.
The document required HTTP authentication.
The document was received in response to a POST.
However, you can modify all of this using the Cache-control header.
If a browser or proxy has stored a document in its cache, it will serve it to a client if the document is not too
old. If it is too old, it will first check with the server to see whether the document has changed. It does this
using an HTTP HEAD request that does not require the server to send the entire document all over again. If the
document has changed, the browser will download a fresh copy of the document. Otherwise, it will serve the
document out of its cache.
Usually for static files you don't have to worry about this, as long as you aren't too picky about how long
browsers cache documents. If you have frequently updated data, or high-performance requirements, you may
want to play with the defaults; but more often than not the defaults are fine. However, content generated
dynamically from PHP, JSP, ASP, CGI, and similar technologies requires you to provide the necessary headers to
enable caching. Otherwise, the page won't be cached.
The easiest tag to send is Last-modified . When a client sees a Last-modified header a reasonable distance into
the past, it assumes the page isn't updated every minute or two and makes a reasonable guess as to how long
it can cache the page. In PHP, you can send this and all other headers with the header function like so:
header('Last-Modified: Thu, 05 Apr 2007 09:41:05 GMT');
You must call the header function before writing any other output to the client.