Caching the output files from a php script not working as expected
<p>I have a php script that sends different images per day. I want that images be cached during the day, but my script is not working as expected because in every request returns 200 OK .</p>

<p>The code is: </p>

<pre><code>&lt;?php

$uno = '1.jpg';
$dos = '2.jpg';
$tres = '3.jpg';
$cuatro = '4.jpg';
$cinco = '5.jpg';
$seis = '6.jpg';
$siete = '7.jpg';

$today=date(l); 

header('Content-Disposition: inline');
header('Content-Type: image/jpg');
header("Content-Transfer-Encoding: Binary");

$expire=60*60*24*1; // seconds, minutes, hours, days
header('Pragma: public');
header('Cache-Control: maxage='.$expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expire) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');


// Find what today is? using date function
if($today==Monday){
readfile($uno);
exit;
}

elseif($today==Tuesday){
readfile($dos);
exit;
}

elseif($today==Wednesday){
readfile($tres);
exit;
}

elseif($today==Thursday){
readfile($cuatro);
exit;
}

elseif($today==Friday){
readfile($cinco);
exit;
}

elseif($today==Saturday){
readfile($seis);
exit;
}

elseif($today==Sunday){
readfile($siete);
exit;
}

?&gt;
</code></pre>

<p>What's wrong with this?</p>

<p><strong>UPDATE:</strong></p>

<p>I've just try refactoring the code as <a href="http://stackoverflow.com/a/13647315/1618163">WebnetMobile.com</a> says, but seems that <em>caching</em> problem keeps alive because it still returns 200 OK.</p>

<p>New code is: </p>

<pre><code>&lt;?php

$today=date("l"); 

header('Content-Disposition: inline');
header('Content-Type: image/jpg');
header("Content-Transfer-Encoding: Binary");

$expire=60*60*24*1;// seconds, minutes, hours, days
header('Pragma: public');
header('Cache-Control: maxage='.$expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expire) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');


// Find what today is? using date function

$map = array( 'Monday' =&gt; '1.jpg',
              'Tuesday' =&gt; '2.jpg',
              'Wednesday' =&gt; '3.jpg',
              'Thursday' =&gt; '4.jpg',
              'Friday' =&gt; '5.jpg',
              'Saturday' =&gt; '6.jpg',
              'Sunday' =&gt; '7.jpg'
             );

readfile( $map[ $today ] );

?&gt;
</code></pre>
