11 Eylül 2007 Salı

If-Modified-Since Header

If-Modifled-Since header is an interesting http request header which saves lot of bandwidth and server resrources. Server must be configured properly to return a proper response for this request header. Intelligent search engine robots like Googlebot can make use of it and this reduces unnecessary crawling of pages which were not updated after last visit.

To configure this first you have to configure last-modified header for all pages. This is the last updated time and date of the particular file. In php you can create a last-modified header like this..

$timestamp=filemtime($_SERVER['SCRIPT_FILENAME']);
$last_modified = substr(date('r', $timestamp), 0, -5).'GMT';
header("Last-Modified: $last_modified");

This takes the time and date of the requested file and returned to the client. (whether it is a browser or a search engine spider)

When a browser or robots get this information it store the time and date to it's internal cache along with the file. In next visit the client add a new request header with the header information called If-Modified-Since with the stored date. That means the client is telling the server that i have a copy of the file dates so and so. Is it modified after that ?

Now the sever has to compare the date with the server file date and should return a response accordingly. If both dates are same then server has to return a '304 Not Modified' response otherwise it has to send the modified file to the client.

With $_SERVER['HTTP_IF_MODIFIED_SINCE'] you can get the if-modified-since header. Check it with isset command and if it is set then you have to compare it with server file date and time. If both are same then return a 304 status code otherwise return the whole file.

For static pages normally server takes care of this process but for dynamic pages (created in PHP, ASP) you have to manually configure it. If not configured, for each requests server has to deliver the whole file and it takes lot of server resources and bandwidth. If cofigured properly server returns only modified files to the client and reduce server overheads.

Apart from this header you can also return an etag with each file and get the request header with if-none-match header. Get the header with $_SERVER['HTTP_IF_NONE_MATCH'] and compare it with the latest etag. You can encrypt the current date and time with md5 command and send it as etag.

Using these headers improves server performance especially you have large database and large number of files. This also reduces page loading time at the browser level because browser has to download only modified files from the server. This will be very useful for returing visitors to your site. For search engine also it helps a lot because it has to download only modified pages and so robots can crawl more pages instead of downloading same pages again and again.

Hiç yorum yok: