Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

that the file is being returned by the cache, try reloading the page quickly. The dates on
the page should remain the same.


Notice that all the dates in these two examples use GMT, or Greenwich Mean Time. This
is specified by the HTTP protocol. Forgetting to convert from your local time zone to
GMT can be an annoying source of bugs.


Listing 18.4 Sending Headers to Encourage Caching


<?
//report actual modification time of script
$LastModified = filemtime(FILE) + date("Z");
header("Last-Modified: ".
gmdate("D, d M Y H:i:s", $LastModified). " GMT");


//set expiration time 24 hours (86400 seconds) from now
$Expires = time() + 86400;
header("Expires: ".
gmdate("D, d M Y H:i:s", $Expires). " GMT");


//tell cache to let page age for 24 hours (86400 seconds)
header("Cache-Control: max-age=86400");
?>




Listing 18.4


The time is GMT



This document was last modified

GMT

It expires
GMT



Setting Document Type


By default, PHP sends an HTTP header specifying the document as being HTML. The
Content-Type header specifies the MIME type text/html, and the browser
interprets the code as HTML. Sometimes you will wish to create other types of
documents with PHP. You will learn in Chapter 19, "Generating Graphics,"
about creating images, which may require an image/png content type. MIME types are
administered by IANA, the Internet Assigned Numbers Authority. You can find a list of

Free download pdf