note 45756 added to function.header
| From: | ondrew at quick dot cz | Date: | Fri, 17 Sep 2004 12:19:06 +0000 |
| Subject: | note 45756 added to function.header | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76875@lists.php.net to get a copy of this message | ||
How to force browser to use already downloaded and cached file.
If you have images in DB, they will reload each time user views them. To prevent this, web server
must identify each file with ID.
When sending a file, web server attaches ID of the file in header called ETag.
header("ETag: \"uniqueID\");
When requesting file, browser checks if the file was already downloaded. If cached file is found,
server sends the ID with the file request to server.
Server checks if the IDs match and if they do, sends back
header("HTTP/1.1 304 Not Modified");
else
Server sends the file normally.
<?php
$file = getFileFromDB();
// generate unique ID
$hash = md5($file['contents']);
$headers = getallheaders();
// if Browser sent ID, we check if they match
if (ereg($hash, $headers['If-None-Match']))
{
header('HTTP/1.1 304 Not Modified');
}
else
{
header("ETag: \"{$hash}\"");
header("Accept-Ranges: bytes");
header("Content-Length: ".strlen($file['content']));
header("Content-Type: {$mime}");
header("Content-Disposition: inline;
filename=\"{$file['filename']}\";");
echo $file['content'];
}
exit();
?>
----
Manual Page -- http://www.php.net/manual/en/function.header.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45756
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45756&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45756&report=yes
Search -- http://master.php.net/manage/user-notes.php