Reputation: 3
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 .
The code is:
<?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;
}
?>
What's wrong with this?
UPDATE:
I've just try refactoring the code as WebnetMobile.com says, but seems that caching problem keeps alive because it still returns 200 OK.
New code is:
<?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' => '1.jpg',
'Tuesday' => '2.jpg',
'Wednesday' => '3.jpg',
'Thursday' => '4.jpg',
'Friday' => '5.jpg',
'Saturday' => '6.jpg',
'Sunday' => '7.jpg'
);
readfile( $map[ $today ] );
?>
Upvotes: 0
Views: 203
Reputation: 2960
I think you should address this from the PHP file generating the HTML, not a file retrieving the image. If you want to load an image on Sunday, another on Monday,.. you can use something like this on the calling page.
echo('<img src="'.date('w').'.jpeg" />');
(or use any other method to find the correct filename shown in this page, like date('l')+array)
This way you let the browser/server handle the caching of real files and display the correct one for the day.
Or
<!other HTML here...>
<img src="<? echo('.date('w').'.jpeg');?>" />
<!other HTML here...>
if file is "mostly HTML".
Caching is a bad idea if you don't want to see Monday image on Tuesday, unless that's what you want, of course.
(if you really want to cache them, then $expires should be= to number of seconds till next midnight)
Upvotes: 0
Reputation: 22820
A brief comment expanding Mark answer above - please refactor your code as:
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');
readfile( date('w') . '.jpeg' );
exit;
after renaming your sunday/domingo file to 0.jpg
- taking advantage of date('w')
.
Upvotes: 1
Reputation: 75635
You need to compare with strings, otherwise you try to compare with nonexisting constant. So it should be
if( $today == 'Monday' ) {
// do someting...
}
but even better, instead of using bunch of if/elseif
, you should use switch/case
switch( $today ) {
case 'Monday':
// do someting...
break;
}
and best, you sould do this smarter way if all you need to do is to just pick up right image:
$map = array( 'Monday' => '1.jpg',
'Tuesday' => '2.jpg',
...
);
readfile( $map[ $today ] );
Upvotes: 1
Reputation: 212522
Monday should be a string, otherwise PHP will treat it as a constant, discover that the constant doesn't exist, issue a warning, and then decide you meant it to be a string.
if($today=="Monday"){
Same applies to all the other weekday names
The warning message is probably messing up the output
Upvotes: 1