Setting precise http cache lifetime independent on local clock?

I’m trying to use some simple http caching mechanism to cache certain pages in the browser for a specified amount of time. For this, I’m using Cache-Control and Expires headers like this:

header("Cache-Control: max-age=15");
header("Expires: ".gmdate("D, d M Y H:i:s", time() + 15)." GMT");

In the above example I want browsers to cache the page for 15 seconds before grabbing it again from the server. This works very well in IE and Opera but Firefox and Chrome skew the caching duration by the difference between the server time and local time where the browser is run.

In my tests I found out that IE and Opera cache the page for 15 seconds while Firefox and Chrome only for 7 seconds. It turned out that there was an 8-second difference between my clock and the server clock.

Next, I tried this:

header("Cache-Control: max-age=5");

and Firefox doesn’t use any cache at all - pretty logical because it’s an equivalent of max-age=-3 taking the time difference into account.

If this is how http cache works in practice then it’s hardly useful in many cases I wanted it to use - especially for short periods where a difference of a few seconds makes a lot of difference. I can’t expect all my site visitors to have clocks synchronised perfectly with a time server - and sometimes the server I’m using is not perfectly synchronised, either.

Is there a way to use http cache accurately across browsers independently of clock differences? IE and Opera seem to be wise enough to calculate the period relatively so that there is no need for the clocks to be synchronised but what to do about Firefox and Chrome?

PS. I’ve noticed that the Expires header is necessary for Opera, but I think this doesn’t change my point.

What you probably want to look at here is E-Tags, that is much better suited to finite cache control than expiration headers.

Most probably I’ll try ETags, however I wanted some reliable method of cache control without any server connection.

It’s weird that even max-age depends on the local clock while logically it should be treated as “x seconds from now”. Hitting against browser bugs as ususal :headbang: