note 61903 added to function.header
| From: | mandor at mandor dot net | Date: | Wed, 15 Feb 2006 01:14:51 +0000 |
| Subject: | note 61903 added to function.header | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-103724@lists.php.net to get a copy of this message | ||
When using PHP to output an image, it won't be cached by the client so if you don't want
them to download the image each time they reload the page, you will need to emulate part of the HTTP
protocol.
Here's how:
<?php
// Test image.
$fn = '/test/foo.png';
// Getting headers sent by the client.
$headers = apache_request_headers();
// Checking if the client is validating his cache and if it is current.
if (isset($headers['If-Modified-Since']) &&
(strtotime($headers['If-Modified-Since']) == filemtime($fn))) {
// Client's cache IS current, so we just respond '304 Not Modified'.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($fn)).'
GMT', true, 304);
} else {
// Image not cached or cache outdated, we respond '200 OK' and output the image.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($fn)).'
GMT', true, 200);
header('Content-Length: '.filesize($fn));
header('Content-Type: image/png');
print file_get_contents($fn);
}
?>
That way foo.png will be properly cached by the client and you'll save bandwith. :)
----
Server IP: 194.246.101.61
Probable Submitter: 83.203.25.117
----
Manual Page -- http://www.php.net/manual/en/function.header.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61903
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61903&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61903&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61903&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61903&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61903&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61903&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61903&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61903&report=yes
Search -- http://master.php.net/manage/user-notes.php