Handles IfModifiedSince and sets Last-Modified date for any page
Usage:
<?php
include("handle304.php");
setModifiedDate( strtotime('11 September 2003') ); // use the date of the content, NOT time() (just an example)
?><?php
include("handle304.php");
clearstatcache();
setModifiedDate( filemtime(__FILE__) ); // use the date of the file itself
?>I'm open to all corrections, additions or neat tricks. Feel free to contact me via the contact-link in the footer.
The code for the module handle304.php:
<?php
function setModifiedDate($contentDate) {
$ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
if ($ifModifiedSince && strtotime($ifModifiedSince) >= $contentDate) {
header('HTTP/1.0 304 Not Modified');
die; // stop processing
}
$lastModified = gmdate('D, d M Y H:i:s', $contentDate) . ' GMT';
header('Last-Modified: ' . $lastModified);
}
?>
Some comments from webado:
As for how I determine the last modified date of a page made up of different included modules, I do a bit of juggling.
Let's say my page.php is like this:
<?php
$module1="module1.inc";
$module2="module2.inc";
$module1="module3.inc";
include("setlastmod.php");
include("template.php");
?>
And let's say the script template.php is an html document with places where I have:
<!DOCTYPE ..> <html> <head> ... ... </head> <body> .. .. include($module1); ... ... ... include($module2); ... ... include($module3); ... </body> </html>
The script setlastmod.php painstakingly (;) ) goes through all of those modules and determines it's last modified date, and I then keeps the latest as the overall last modified date for the page where they are getting used.
The script setlastmod.php is like this:
<?php
clearstatcache();
$lm1 = filemtime($module1);
clearstatcache();
$lm2 = filemtime($module2);
clearstatcache();
$lm3 = filemtime($module3);
$lastmod = max($lm1,$lm2,$lm3);
$last_modified = gmdate('D, d M Y H:i:s', $lastmod).' GMT';
header("Last-Modified: $last_modified");
?>
I am currently setting the header for the last modified date according to this method.