last-modified | Dmytro Shteflyuk's Home https://kpumuk.info In my blog I'll try to describe about interesting technologies, my discovery in IT and some useful things about programming. Mon, 07 Sep 2015 23:42:07 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 Correct Last-Modified header processing https://kpumuk.info/php/correct-last-modified-header-processing/ https://kpumuk.info/php/correct-last-modified-header-processing/#comments Mon, 10 Apr 2006 20:54:25 +0000 http://kpumuk.info/php/correct-last-modified-header-processing/ 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 […]

The post Correct Last-Modified header processing first appeared on Dmytro Shteflyuk's Home.]]>
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// --- 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):

1
2
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

The post Correct Last-Modified header processing first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/php/correct-last-modified-header-processing/feed/ 12