Forum Moderators: coopster

Message Too Old, No Replies

A small cache system using example.com/?js=myscript.js

PHP code to deal with 304/200/404 HTTP response

         

tomda

6:19 am on Oct 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't have mod_header on my shared severs so I have decided to call all my js, img, etc. through the index.php which then deal with the HTTP respond (e.g. example.com/?js=myscript.js)

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

<?php

if(!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();
}
?>

dreamcatcher

6:15 am on Oct 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for sharing your code Tomda. I`m guessing as no-one has picked any faults, it must be ok. ;)

dc

tomda

6:02 am on Oct 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you Dreamcatcher for clearing unanswered post.

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