Caching
For those unfamiliar with cacheing:
Web caching is the temporary storage of web objects (such as HTML documents) for later retrieval. There are three significant advantages to web caching: reduced bandwidth consumption (fewer requests and responses that need to go over the network), reduced server load (fewer requests for a server to handle), and reduced latency (since responses for cached requests are available immediately, and closer to the client being served). Together, they make the web less expensive and better performing.
taken from www.web-caching.com
How to do use cache control with PHP
The client – web browser, spider, cache etc.
having requested a page, receives all the cache control information in the headers sent before the content of the page is sent. By default PHP pages send no cache control headers, and subsequently are not cacheable.
In order for a page to be cacheable, it must have at least a last-modified header, and preferable a few more headers – expires, etag (more about this below). Since the php script dynamically generates the page the moment it is requested, the last modified time is the instant it is requested.
But lets say you have a news article page, so you want to send a last modified time when the news article was composed, then we send the headers with the php header command
//set the time to 15-02-2004
$cache_time = mktime(0,0,0,15,2,2004);
header("last-modified: " . gmdate("D, d M Y H:i:s",$cache_time) . " GMT");
//now send the article text
for($i=0;$i<9999;$i++) echo "blah blah blah";
Comprehensive example
Chances are you'll probably have a whole database of articles all with different modified dates. With any luck you'll have a timestamp type field in the table the article is stored in, which will update each time the record is modified. But without further ado, here's the listing for a comprehensive cacheing script.
//cache script
//start output buffering so nothing is sent until the end
ob_start();
//set cache time= last modified, this could be read from a database
$cache_time = mktime(0, 0, 0, 1, 1, 2004);
//contents of the page
echo "";
echo "Cache test: $cache_time";
echo "";
//set time to expire in seconds
$expires = 5000;
//get the contents of the buffer into a variable
$body = ob_get_contents();
ob_end_clean();
//Etag and last modified checking and Generation
$send_body = true;
$etag = '"' . md5($body) . '"';
header ("ETag: " . $etag );
header("last-modified: " . gmdate("D, d M Y H:i:s",$cache_time) . " GMT");
$inm = split(',', getenv("HTTP_IF_NONE_MATCH"));
foreach ($inm as $i)
{
if (trim($i) == $etag || trim($i) == $cache_time)
{
header ("HTTP/1.0 304 Not Modified");
$send_body = false;
}
}
//last modified test
if(getenv("HTTP_IF_MODIFIED_SINCE") == gmdate("D, d M Y H:i:s",$cache_time). " GMT")
{
header ("HTTP/1.0 304 Not Modified");
$send_body = false;
}
//more headers
header("Expires: " . gmdate("D, d M Y H:i:s",$cache_time+$expires) . " GMT");
header("Cache-Control: max-age=$expires, must-revalidate");
header('Content-Length: ' . strlen($body));
//if we're not cacheing
if($send_body) print $body;
Leave a Reply