Jump to content
  • Advertisement
Sign in to follow this  
BeanDog

[web] Proper use of "HTTP/1.1 304 Not Modified" in PHP serving images?

This topic is 4349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've started serving image files through PHP scripts via the magic of mod_rewrite. The URL is rewritten to the script without informing the browser (internal proxy), so in my mind, the browser shouldn't act any differently. But in practice, I found that every time I loaded a page, the browser would reload every image. Upon inspection via the Live HTTP Headers plugin in FireFox, I found that normally images respond with a HTTP/1.1 304 Not Modified, while I sent a normal HTTP/1.1 200 OK response. As a consequence, the browser re-requested each image every time. I've googled around a bit for proper use of the 304 response, but I can't find much useful. Does anyone know how to keep the browser from refreshing these images? I'm using PHP.

Share this post


Link to post
Share on other sites
Advertisement
Note that using sessions can adjust which caching options are used. (See session.cache-limiter.) Using header() (after session_start() if using sessions) will allow you to fine tune the cache settings. In general, setting private or public caching should automatically just work.

However, you can use the Last-Modified if the browser is sending it. Basically, you have to look at the incoming request (probably via $_SERVER or apache_request_headers()) and check to see if the last fetched date is greater than when the image was last modified. If the image hasn't been changed, you simply send the Not Modified response and don't send any data.

Share this post


Link to post
Share on other sites
Rather than serving 304 responses, you should start enabling the browser cache by:

1. Send a Last-modified header
2. If possible, send an "Expires" header in the future- it's up to you to decide when a given image should expire. If it's "never", set it a long way if you want.
3. Make sure that nothing is setting any headers which inhibit caching - this includes session stuff. If your images are publicly accessible to everyone equally, don't use sessions at all on that page.

Then the browser should be able to store them in its own cache.

The proper use of the "304 not modified" repsonse, is if the browser sends a conditional request, with one of the conditional headers such as "If-Modified-Since:". If you want you can send 304 back if the file hasn't been modified.

Conditional requests are sent if the browser has the file in its cache and it hasn't expired, but it has some other reason to think that it might be out of date- this varies from browser-to-browser, although a common reason is that the user pressed "refresh".

Some browsers will always send a conditional request if the cached file is from a previous session (i.e. the user restarted their browser since it was last fetched).

Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!