Correct Last-Modified header processing

Apr 10
2006 22:54 (Development, PHP) · Русский (22,492 views)

This is quick post about Last-Modified header. Please imagine following situation: you have image stored in your database and you need to send it to the browser on some request. But image extraction from database takes some time, and if there are more than one image you Web-server’s productivity will decrease dramatically. Is this case you need to implement caching functionality in your application. All images can be changed therefor you need to have ability to check image modified date (for example, this date can be stored in same database).

Every browser has its own cache, and if you will send right Cache-Control header, your image will be cached for some time. After cache time expiring browser will send request to your application to get fresh version of image. But if your image was not updated you can send 304 code in response to tell browser about this. Look at the following code snippet:

// --- Your code ---

// You need past following before any output

// here you need to select modified date from DB in UNIX timestamp format
// (same as time() function in PHP or UNIX_TIMESTAMP() function in MySQL)
$date = time();

$last_modified = gmdate('D, d M Y H:i:s', $date) . ' GMT';
// did the browser send an if-modified-since request?
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  // parse header
  $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);

  if ($if_modified_since == $last_modified) {
    // the browser's cache is still up to date
    header('HTTP/1.0 304 Not Modified');
    header('Cache-Control: max-age=86400, must-revalidate');
    exit;
  }
}

header('Cache-Control: max-age=86400, must-revalidate');
header('Last-Modified: ' . $last_modified);

// --- Your code ---

In this code I check HTTP_IF_MODIFIED_SINCE request variable which will be sent by browser when cache is expired. If image was not modified, special header will be sent. Default cache period in this sample is 24 hours (86400 seconds). This is very simple and powerful issue which can be used in your Web-application as-is to increase perfomance.

Hint: you can create static-like URLs using following lines in .htaccess file (mod_rewrite):

RewriteEngine On RewriteRule ^image_([0-9]+).php$ getimage.php?image=$1 [L]

In this case your images will have URLs like http://example.com/image_10.php, and all request will be redirected to getimage.php with parameter image=10.

Useful links

11 Responses to 'Correct Last-Modified header processing'

Subscribe to comments with RSS or TrackBack to 'Correct Last-Modified header processing'.

1
said on 2006-04-10 at 10.59 pm

Thanks for useful post! I’m working now on simple data caching system and I will use this information in cache validation part.

Great thanks! Keep writing such interesting and usefull stuff!

2
Валерий Пешков
said on 2006-05-19 at 6.53 am

Дмитрий большое спасибо за статью! Небольшое замечание. Мне кажется нужно использовать time() вместо date()?

3
said on 2006-05-19 at 11.11 am

Валерий, большое спасибо за комментарий, Вы действительно правы, использовать нужно функцию time(). В заметке исправил.

4
Oleg
said on 2006-05-26 at 10.08 am

Для $_SERVER['HTTP_IF_MODIFIED_SINCE'] лучше, imho, использовать strip_tags() или что-то подобное

5
Alexander
said on 2006-10-30 at 12.47 am

Спасибо. То что доктор прописал…

6
said on 2007-01-15 at 12.25 pm

Спасибо, Дмитрий. Полезная статья, замечательный блог.

7
peregrino
said on 2007-09-06 at 3.59 pm

But still the *same image* is requested to the database multiple times (one for the first time each browser requests that image). It would be great if we could order to the web server to cache the images it’s sending to the browsers.

without a server cache, storing images in a database still introduces some penalties over storing them in the file system.

8
said on 2007-11-27 at 1.34 am

I tried out your code, and have a problem, maybe someone can help. When I try it locally on my ubuntu box, it works great. Teh site loads instantly if cached.

But when I tried it on my actual server (dreamhost) it didn’t work at all, the images disappeared. Humm

Did that happen to anyone?

9
Oleg
said on 2008-03-22 at 12.21 pm

А этот код подходит для динамического контента, где данные вытаскиваются из базы (WORDPRESS)?

10
Oleg
said on 2008-03-22 at 12.25 pm

Просто стоит задача удовлетворить пожелания Яндекса по выдаче кода 304 на запрос «If-Modified-Since». Не знаю как это сделать, если не сложно, подскажите, пожалуйста. Спасибо.

11
said on 2008-05-29 at 2.28 am

А насколько адекватно яндекс оценивает заголовок last-modified?

Post a comment

You can use simple HTML-formatting tags (like <a>, <ul> and others). To format your code sample use <code lang="php">$a = "hello";</code> (allowed languages are ruby, php, yaml, html, csharp, javascript). Also you could use <code>$a = "hello";</code> and its syntax would not be highlighted. If you are not using <code> tag, replace < sign with &lt;.

Submit Comment

 
Copyright © 2005 - 2008, Dmytro Shteflyuk