Posted The: 21/06/2009 At: 12:49
Contents:
The IF_MODIFIED_SINCE http request-header, is a header which is sent by most browsers. The header basically asks the server if the page was modified after the last visit, if that was the case, the resource is downloaded again.
If the requested resource wasn't modified since the last visit, the server returns a 304 Not Modified http Response code, and the resource is not downloaded.
There however is a difference when dealing with static pages and dynamic pages. The IF_MODIFIED_SINCE header is normally set by the server, but when you use a server-sided scripting language, like php, then you must set these on your own. This Article Explains how to do this in your PHP Scripts
Normally you wont have to do anything with static pages, just make sure your server support the IF_MODIFIED_SINCE header. I won't cover how to do this in this Tutorial, but maybe I'll get back to that later.
This includes websites made with server-sided scripting, such as PHP, and/or programming. These pages normally don't send the correct headers, unless they are coded, or programmed to do so.
Below is an example of how to use IF_MODIFIED_SINCE with PHP.
$fp = fopen($_SERVER["SCRIPT_FILENAME"], "r");
$etag = md5(serialize(fstat($fp)));
fclose($fp);
header('Etag: '.$etag);
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $SelectS['LTIME'])." GMT");
header("Etag: $etag");
You should note that the $SelectS['LTIME'] Variable holds the timestamp from the LTIME field in your database, this is the date/time at which the requested content was last modified. The timestamp is saved in the database, using the unix format with php time().
Note. Its a good idea to have a field in your table, for both the date of postage, and the date of which the entry was last edited.
Now its just a PHP if then else, situration. I.e.
if ((@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $SelectS['LTIME']) && (
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)) {
header("HTTP/1.1 304 Not Modified");
exit;
}
The Etag header is usually sent to the browser by the server, and is used to check if the requested resource has changed since the last visit. The browser then sends this back to the server, contained in the HTTP_IF_NONE_MATCH request header.
Developers can use the Etag Header, to ensure that the script itself didn't change. This is usually done by generating an md5 hash from the content of the script, and sending it in the Etag header. But it could in theroy contain anything the developer decide.
The full code required, excluding the database connection, and related queries. Is shown below:
$fp = fopen($_SERVER["SCRIPT_FILENAME"], "r");
$etag = md5(serialize(fstat($fp)));
fclose($fp);
header('Etag: '.$etag);
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $SelectS['LTIME'])." GMT");
header("Etag: $etag");
if ((@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $SelectS['LTIME']) && (
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)) {
header("HTTP/1.1 304 Not Modified");
exit;
}
You should place this somewhat in the top of your source code, either directly in the script, or as an include. And before any output is sent to the browser, including http headers.
The main benefits are related to bandwidth usage, sites that make use of the IF MODIFIED SINCE Header, generally save a lot of bandwidth.
Search engines that use this header, will also benefit, in that, they won't have to waste resources crawling pages that didn't change. And thats why some of them may say to make sure that your server supports the HTTP IF MODIFIED SINCE Header.
Comments: [0]
© Brugbart Webdesign