note 57707 added to function.header
| From: | denilson at vialink dot com dot br | Date: | Tue, 11 Oct 2005 23:24:24 +0000 |
| Subject: | note 57707 added to function.header | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96872@lists.php.net to get a copy of this message | ||
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).
----
Manual Page -- http://www.php.net/manual/en/function.header.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57707
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57707&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57707&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57707&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57707&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57707&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57707&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57707&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57707&report=yes
Search -- http://master.php.net/manage/user-notes.php