Forum Moderators: coopster
I wrote the code below and would like your comment; it workds, but I want to make that it is correct... so did I miss something ? And also, do not hesitate to use it if needed.
Thank you
<?phpif(!empty($_GET['js'])) {
// GET VAR
$js = $_GET['js'];
// GET TIME
$offset = 3600 * 24 * 365;
$gmt_mtime = gmdate('D, d M Y H:i:s', time() )." GMT";
// GET PATH
$server_path = $_SERVER[DOCUMENT_ROOT].str_replace("index.php", "", $_SERVER[PHP_SELF]);
$js_path = $server_path.$js;
if (file_exists($js_path) && is_readable($js_path)) {
// GET EXTENSION
$ext = substr($js_path, -2);
switch ($ext) {
case "js": $mime = 'application/x-javascript'; break;
default: $mime = false; break;
}
if (!empty($mime)) {
// GET LAST MODIFIED TIME
@clearstatcache();
$last_modified_time = filemtime($js_path);
// GET ETAGS
$etag = md5_file($js_path);
if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==$last_modified_time) ¦¦ (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH'])==$etag)) {
// RETURN 304
header("HTTP/1.1 304 Not Modified");
exit();
} else {
// RETURN 200 WITH ETAGS, EXPIRE DATE, ETC.
header("Content-type: ".$mime);
header("Content-length: ".filesize($js_path));
header("Expires: ".gmdate("D, d M Y H:i:s", time() + $offset)." GMT");
header("Accept-Encoding: gzip, deflate");
header("Cache-Control: public, max-age=".$offset.", s-maxage=".$offset."");
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("ETag: ".$etag);
// OPEN FILE
$fn = @fopen("./".$js, "r");
if($fn) {fpassthru($fn);}
exit();
}
} else {
// RETURN 404
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
exit();
}
} else {
// RETURN 404
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
exit();
}
} else {
// RETURN 404
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
exit();
}
?>
The code is good although it works on my local server but not on my, host, it seems that $_SERVER['HTTP_IF_MODIFIED_SINCE'] and $_SERVER['HTTP_IF_NONE_MATCH'] don't exist (the request header on server doesn't have a If-Modified-Since and If-None-Match)
Please read [webmasterworld.com...] forget the lenghty post...
I am working on Curl function currently to get header data, nonetheless I would love to get an answer to the last three tiny question ?
Basically, it is a good working method and will it slow down the server ?
Thanks