KBD

Keith Devens .com

Thursday, August 28, 2008 Flag waving
It's such a fine line between stupid and clever. – David St. Hubbins (This is Spinal Tap)
← del.icio.us links now cron'dCaching →

Daily link icon Wednesday, May 5, 2004

Conditional get

In the spirit of Simon's, I created my own conditional get function. My RSS feed finally does conditional get Smiley

<?php
function httpConditionalGet($timestamp$is_timestamp true){
    if(
$is_timestamp$last_modified gmdate('r'$timestamp);
    if((isset(
$_SERVER['HTTP_IF_NONE_MATCH']) and $_SERVER['HTTP_IF_NONE_MATCH'] == $timestamp) or
        (
$is_timestamp and
        isset(
$_SERVER['HTTP_IF_MODIFIED_SINCE']) and
        
$_SERVER['HTTP_IF_MODIFIED_SINCE'] == $last_modified)
    ){
        
header('HTTP/1.1 304 Not Modified');
        exit;
    }
    if(
$is_timestampheader("Last-modified: $last_modified");
    
header('ETag: '.($is_timestamp $timestamp md5($timestamp)));
}
?>

So, you can pass it a timestamp and it'll use Last-modified headers, but if you explicitly tell it it's not a timestamp then it won't and will just use an ETag instead.

Update (5/7): Note that this code is kind of broken. It was just a quick hack that worked for me, but I didn't test it well because I was (and still am) in the middle of finals. Once I get a chance I'll revise it and fix what's broken. Thanks to Bob Congdon for pointing me to what I did wrong. I explain some of the things in the comments on his post about this.

Update (12/11): I figured I'd post the updated, correct version of this code:

<?php
function httpConditionalGet($thing$is_timestamp true){
    
#thing can be either a timestamp or anything else you want to serve as an etag
    #if it's not a timestamp, it's the caller's responsibility to md5 it or whatever
    
if($is_timestampheader('Last-modified: '.($lm gmdate('r'$thing)));
    
header('ETag: "'.$thing.'"');
    
$client_etag = isset($_SERVER['HTTP_IF_NONE_MATCH'])     ? $_SERVER['HTTP_IF_NONE_MATCH']     : NULL;
    
$client_lm   = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : NULL;

    if((
$client_etag and $client_etag == '"'.$thing.'"'#etag
       
or
       (
$is_timestamp and $client_lm and ($client_lm == $lm or $thing == strtotime($client_lm))) #last-modified
    
header('HTTP/1.1 304 Not Modified') and exit;
}
?>

It looks much better when it's not word-wrapped.

← del.icio.us links now cron'dCaching →

Comments XML gif


Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

August 2008
SunMonTueWedThuFriSat
 12
3456789
10111213141516
17181920212223
24252627282930
31 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 1 posts

Recent comments XML

Girls, please don't get breast implants

Wow, After all this time, the​comments on this page continue to​grow. It wa...

Ajeet: Aug 25, 2:36am

Generated in about 0.171s.

(Used 8 db queries)

mobile phone