note 57707 deleted from function.header by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 17:36:12 +0000 |
| Subject: | note 57707 deleted from function.header by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176219@lists.php.net to get a copy of this message | ||
Note Submitter: denilson at vialink dot com dot br
----
If you want to enable caching of your page, you have two solutions: use Etag or use Last-Modified.
Someone has already posted here the code to use Etag. However, sometimes, it is easier (better) to
use Last-Modified. The full code is here:
<?php
function get_http_mdate()
{
return gmdate("D, d M Y H:i:s",filemtime($SCRIPT_FILENAME))." GMT";
}
function check_modified_header()
{
// This function is based on code from http://ontosys.com/php/cache.html
$headers=apache_request_headers();
$if_modified_since=preg_replace('/;.*$/', '',
$headers['If-Modified-Since']);
if(!$if_modified_since)
return;
$gmtime=get_http_mdate();
if ($if_modified_since == $gmtime) {
header("HTTP/1.1 304 Not Modified");
exit;
}
}
check_modified_header();
header("Last-Modified: ".get_http_mdate());
?>
The script checks if time from "If-Modified-Since" header is equal to current
modified-time. If it is, a 304 code is returned, and PHP exits. If it is not, PHP continues
normally.
You may want to change how "get_http_mdate()" function gets the time. You may want to get
time from another file, or from somewhere else (like a date field on database).