ETags and $_SERVER['HTTP_IF_NONE_MATCH']

I’m using GD to do stuff with images and trying to include a bit of caching:

$imagePath = "path/to/some/image";

$eTag = $imagePath;
$eTag .= fileMTime($imagePath);
$eTag = md5($eTag);

if((isset($_SERVER['HTTP_IF_NONE_MATCH'])) && (stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == $eTag)) {
	header("HTTP/1.1 304 Not Modified", TRUE, 304);
	exit();
}

header("ETag: ".$eTag);
header("Content-Type: image/png");
readFile($imagePath);
exit();

My problem is that $_SERVER[‘HTTP_IF_NONE_MATCH’] keeps coming back NULL. (Or, more correctly, it doesn’t come back at all.) I’ve tried in both Firefox and Safari.

I know the ETag is being sent because I ran it through web-sniffer.net and got the following details:

HTTP Response Header

HTTP Status Code:  HTTP/1.1 200 OK

Date:  Fri, 16 Mar 2007 05:08:07 GMT

Server:	 Apache/1.3.37 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.28 OpenSSL/0.9.7a PHP-CGI/0.1b

Cache-Control:  no-store, no-cache, must-revalidate, post-check=0, pre-check=0

ETag:  540e7670b785a45880075361615df7d2

Expires:  Thu, 19 Nov 1981 08:52:00 GMT

Pragma:  no-cache

X-Powered-By:  PHP/5.2.1

Set-Cookie:  PHPSESSID=360ab7029b73f86e949790bcbd7b1c59; path=/

Connection:  close

Transfer-Encoding:  chunked	

Content-Type:  image/png

The same script generates several different images on each page using different parameters, eg. <img src=‘displayImage.php?imageID=myImage’ />, so I’m wondering if perhaps the browser chops off the query string and is getting conflicting ETags from the same file? But I’ve tried loading the same image several times over with still no success.

Is there somebody out there who has a better idea of how this stuff is supposed to work? I’d appreciate your help :slight_smile:

OK, I thought about it a bit more and realised that I actually have to tell the browser to cache the image in the first place in order for this to work. So now I have success in Firefox, but still not in Safari.

Why is it that all the tutorials about caching assume that you DON’T want your content cached?