[eluser]louis w[/eluser]
Not directly code igniter but I thought I would ask this here to see if anyone can help out since I'm not getting replies elsewhere.
I am serving images through php and having some problems setting it up to respond with 304 headers to save on load time.
Most of the code below I found on php.net. It works, however ALWAYS responds with 200. For some reason
the If-Modified-Since header is not being sent on any requests even though I am sending the Last-Modified header initially. This is being done on an apache server. Any idea what might be wrong?
Code:
// Get headers sent by the client.
$headers = apache_request_headers();
$file_time = filemtime($load_path);
log_message('debug', '!! Headers: '.print_r($headers, true));
if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == $file_time)) {
log_message('debug', '!! Doing 304');
// Client's cache IS current, so we just respond '304 Not Modified'.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $file_time).' GMT', true, 304);
} else {
log_message('debug', '!! Doing 200');
// Image not cached or cache outdated, we respond '200 OK' and output the image.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $file_time).' GMT', true, 200);
header('Content-Length: '. filesize($file['paths']['server']));
header('Content-type: '. get_mime_by_extension($load_path));
readfile($load_path);
}