Re: 304 Not Modified
| From: | Christian Reiniger | Date: | Thu, 06 Sep 2001 09:53:50 +0000 |
| Subject: | Re: 304 Not Modified | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-66189@lists.php.net to get a copy of this message | ||
On Wednesday 05 September 2001 21:22, Ouster wrote:
> I'm making a sort of cache system. I fell to this problem: I leave a
> cookie with the timestamp of the last access, and when the user
> reconnect, I compare the timestamp of the last change with the
> timestamp sent me with the cookie. Then I leave a new cookie.
>
> So:
> if($last_modified <= $last_access)
> {
> header("304 Not Modified);
> exit;
> }
> else
> {
> /* code genertating the new page */
> }
Tip: You don't even need a cookie for that. If you send a
"Last-Modified: " header along with all your pages (and perhaps even if
you don't), the browser will automatically add an "If-Modified-Since:"
header on each request. See the attached files for how to handle this
> Whether the If is TRUE, I get a blank page, else I see a new page
> (correct behaviour).
> Why I always get a blank page and not the cached page?
(see other responses)
--
Christian Reiniger
LGDC Webmaster (http://lgdc.sunsite.dk/)
/* you are not expected to understand this */
- from the UNIX V6 kernel source
<?php /** * Functions for HTTP info handling * * Authors : Christian Reiniger <creinig@mayn.de> * License : GNU GPL V2 or later (http://www.fsf.org/copyleft/gpl.html) * * Last change by $Author$ * on $Date$ * to $Revision$ */ /** * Display the "404 - Not Found" page */ function pbHTTP_404 () { global $pbGlobals; header ("HTTP/1.0 404 Not Found"); include ($pbGlobals->Inc404); exit (); } /** * Send a "304 - Not Modified" response */ function pbHTTP_304 () { header ('HTTP/1.0 304 Not Modified'); header ('Date: ' . gmdate ('D, d M Y H:i:s') . ' GMT'); exit (); } /** * Get the value passed in an "If-Modified-Since" header or -1 if none set * Returns a Unix timestamp; */ function pbHTTP_GetModifiedSince () { foreach (getallheaders () as $Key => $Val) { if ($Key == 'If-Modified-Since') { return strtotime (trim ($Val)); } } return -1; } /** * Send a "Last-Modified" header */ function pbHTTP_LastMod ($Unixtime = 0) { if ($Unixtime == 0) $Unixtime = time (); $TimeS = gmdate ('D, d M Y H:i:s', $Unixtime); header ("Last-Modified: $TimeS GMT"); } /** * Send headers to disallow caching */ function pbHTTP_NoCache () { header ('Expires: -1'); header ('Cache-Control: no-cache'); header ('Pragma: no-cache'); } ?>Attachment: [text/x-c++] includes/pages/cached.php cached.php
<?php /** * Functions for HTTP info handling * * Authors : Christian Reiniger <creinig@mayn.de> * License : GNU GPL V2 or later (http://www.fsf.org/copyleft/gpl.html) * * Last change by $Author$ * on $Date$ * to $Revision$ */ /** * Display the "404 - Not Found" page */ function pbHTTP_404 () { global $pbGlobals; header ("HTTP/1.0 404 Not Found"); include ($pbGlobals->Inc404); exit (); } /** * Send a "304 - Not Modified" response */ function pbHTTP_304 () { header ('HTTP/1.0 304 Not Modified'); header ('Date: ' . gmdate ('D, d M Y H:i:s') . ' GMT'); exit (); } /** * Get the value passed in an "If-Modified-Since" header or -1 if none set * Returns a Unix timestamp; */ function pbHTTP_GetModifiedSince () { foreach (getallheaders () as $Key => $Val) { if ($Key == 'If-Modified-Since') { return strtotime (trim ($Val)); } } return -1; } /** * Send a "Last-Modified" header */ function pbHTTP_LastMod ($Unixtime = 0) { if ($Unixtime == 0) $Unixtime = time (); $TimeS = gmdate ('D, d M Y H:i:s', $Unixtime); header ("Last-Modified: $TimeS GMT"); } /** * Send headers to disallow caching */ function pbHTTP_NoCache () { header ('Expires: -1'); header ('Cache-Control: no-cache'); header ('Pragma: no-cache'); } ?>Attachment: [text/x-c++] includes/pages/cached.php cached.php