OK, I think I got it (it was more code than I had thought it would
be).  This does seem to be working.

On initial load, I get a status 200, then wait a while and it cycles
through 304s (nothing new to fetch).  Cause a change to my test.txt
file on the server and the next time it fetches the new version with a
200 status.  Wait a while and it cycles through 304s (nothing new to
fetch).  Cause another change to my test.txt file on the server and it
fetches the new version with a 200 status.  Rinse and repeat.

Does this look proper to you guys?

<?php
//CONDITIONAL GET - we only want to fetch this file if it has changed
since the last time it was fetched

//this will return a  unix timestamp value like 1229624249
$lastmod = filemtime("test.txt");

//turn it into a proper GMT date, i.e., Thu, 18 Dec 2008 19:28:28 GMT
$lastmod = gmdate('D, d M Y H:i:s', intval($lastmod)) . ' GMT';

//a unique indentifier for the version of the modified file
//the contents of "ETag" should change whenever the page changes.
$etag = '"' . md5($lastmod) . '"';

// ETag is sent even with 304 header
header("ETag: $etag");

//check if the browser sent the appropriate headers and see if they
match to the ones we are about to send.
//the headers which contain the last modification date and the etag
are "If-Modified-Since" and "If-None-Match".
$ifmod = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
        ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $lastmod
        : null;

$iftag = isset($_SERVER['HTTP_IF_NONE_MATCH'])
        ? $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
        : null;

//if either matches and neither is a mismatch, send not modified
header
if (($ifmod || $iftag) && ($ifmod !== false && $iftag !== false)) {
        header('HTTP/1.0 304 Not Modified');
        die;
}

// Last-Modified doesn't need to be sent with 304 response
        header("Last-Modified: $lastmod");
?>

On Dec 18, 11:15 am, Magnificent
<imightbewrongbutidontthin...@gmail.com> wrote:
> I'm making some progress, if I include the following:
>
> <?php
> $last_modified = filemtime("test.txt");
> header("Last-Modified: " . $last_modified);
> ?>
>
> I get a response header with:
> Last-Modified   1229624249
>
> If I then wait a bit and make a change to cause test.txt to be
> updated, I get:
>
> Last-Modified   1229627412
>
> So I'm definitely getting the Last-Modified header sent but all the
> responses still show up as 200.  I should be getting 304s until
> test.txt is updated, then get one 200, then more 304s until test.txt
> is update again, right?
>
> On Dec 18, 11:05 am, Mike Alsup <mal...@gmail.com> wrote:
>
> > > I take it back, my livedata_fetch.php is coming back with a 200
> > > status, but I want it coming back with a 304 not modified, right?
> > > That means it'll only fetch the file if it's been updated since the
> > > last time it was fetched?
>
> > Right.  The server needs to set the Last-Modified header for this to
> > work correctly.  If it does, jQuery will use that date/time in the If-
> > Modified-Since header.  If the resource has not been modified then the
> > server should return an empty response body with a 304 status.

Reply via email to