Uli's Web Site
[ Zathras.de - Uli's Web Site ]
Other Sites: Stories
Abi 2000
Stargate: Resurgence
Lost? Site Map!
     home | blog | moose | programming | articles >> blog

 Blog Topics

15 Most Recent [RSS]

 Less work through Xcode and shell scripts
2011-12-16 @600
 iTunesCantComplain released
2011-10-28 @954
 Dennis Ritchie deceased
2011-10-13 @359
 Thank you, Steve.
2011-10-06 @374
 Cocoa Text System everywhere...
2011-03-27 @788
 Blog migration
2011-01-29 @520
 All you need to know about the Mac keyboard
2010-08-09 @488
 Review: Sherlock
2010-07-31 @978
 Playing with Objective C on Debian
2010-05-08 @456
 Fruit vs. Obst
2010-05-08 @439
 Mixed-language ambiguity
2010-04-15 @994
 Uli's 12:07 AM Law
2010-04-12 @881
 Uli's 1:24 AM Law
2010-04-12 @874
 Uli's 6:28 AM Law
2010-04-12 @869
 Uli's 3:57 PM Law
2010-04-12 @867


Now supporting if-modified-since !

For quite a while, I've been meaning to add a full-text RSS feed to my site. There's one obstacle, though: Since I instated it, the RSS feed was the top bandwidth hog on my site. A full-text feed would mean that, every time someone's RSS reader checks whether I've written something new on this site, it would download all articles. I simply didn't want to take that hit, especially in light of the upcoming new version of the Moose, which tends to eat bandwidth like there's no tomorrow. But then, in the feature description of one blogging engine, I read that HTTP supports just sending back a "this page is unchanged" reply. So, I added that. Read on to learn how:

The first thing every sensible programmer will do when faced with something new is to google it. So I did, and apart from RFC 2616 "HTTP 1.1", it brought up a forum entry at WebmasterWorld containing some nice sample code, and I also remembered PHP's header function, which has user-comments that show how to generate the proper reply.

So, how does it work? Glad you asked! When a browser requests your web page, it can send either a regular GET request, or if it has a cached copy of your page, it can send a conditional GET request instead. The difference between the two is simply that the conditional request contains an additional If-Modified-Since: header that contains the date of the cached copy. All your code has to do is compare that date to the change date of the page you're about to display. If they don't match, send back a 304 not modified "error" and don't send the page. That will tell the browser it's okay to show the cached response.

If your page has changed, just send the page like you used to do. Of course, to make sure the browser has the correct data, you should also send along a Last-Modified: header with every successful request. That's all. The code to do this that I'm using is:

if( $cond && ($_SERVER['REQUEST_METHOD'] == 'GET')
  && (strtotime( $cond ) >= $aw_newestdate) )
    header( "HTTP/1.0 304 Not Modified" );

header( "Last-Modified: " . gmdate( "D, d M Y H:i:s", $aw_newestdate ) . " GMT" );

// echo page contents here.

Where $aw_newestdate is a variable containing a Unix timestamp indicating when the page was last changed.

Now, I haven't had much time to test this, and some browsers simply don't send conditional requests, but if everything works as intended, I'll notice a drop in bandwidth consumption in next month's server statistics. And if that drop is large enough, I may just get to give you that full-text RSS feed we all want so much...
Created: 2005-07-27 @810 Last change: 2005-07-27 @861 | Home | Admin | Edit
© Copyright 2003-2023 by M. Uli Kusterer, all rights reserved.