Note that there are some explanatory texts on larger screens.

plurals
  1. POCaching image requests through PHP - If-Modified-Since not being sent
    primarykey
    data
    text
    <p>I am serving images through php and having some problems setting it up to respond with 304 headers to save on load time.</p> <p>Most of the code below I found on php.net. It works, however ALWAYS responds with 200. <strong>For some reason the If-Modified-Since header is not being received on any requests even though I am sending the Last-Modified header initially.</strong> This is being done on an apache server. Any idea what might be wrong?</p> <p><a href="http://louiswalch.com/beta/304_test/" rel="noreferrer">Example here.</a></p> <p>This page will load the image from disk and display it to browser, along with sending a Last-Modified header. If you refresh the page, the browser doesn't send a If-Modified-Since header like it should.</p> <pre><code>define('SITEPATH', (dirname($_SERVER['SCRIPT_NAME']) == '/') ? '/' : dirname($_SERVER['SCRIPT_NAME']).'/'); $load_path = $_SERVER['DOCUMENT_ROOT'] . SITEPATH . 'fpo_image.jpg'; // Get headers sent by the client. $headers = apache_request_headers(); $file_time = filemtime($load_path); header('Cache-Control: must-revalidate'); header('Last-Modified: '.gmdate('D, d M Y H:i:s', $file_time).' GMT'); if (isset($headers['If-Modified-Since']) &amp;&amp; (strtotime($headers['If-Modified-Since']) == $file_time)) { header('HTTP/1.1 304 Not Modified'); header('Connection: close'); } else { header('HTTP/1.1 200 OK'); header('Content-Length: '. filesize($load_path)); header('Content-type: image/jpeg'); readfile($load_path); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload