0

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 ] );

?>
2
  • Caching is suspicious here in my opinion. If someone visits your page on monday at 11:00PM (23:00) and caching worked as you expected (keeping it in cache 24h), if he visits again next day (tuesday) morning he will se monday image on tuesday. Is this you want? Commented Nov 30, 2012 at 15:20
  • Anyway, have you considered that the browser may reload the image anyway if you hit the Refresh button? The only way to test if it is in cache is to get to the page with the image by navigating with links. And even then the browser is still free to decide if he wants to reload or just get the header of the page or nothing at all. Also make sure to check the options on cache of your browser Commented Nov 30, 2012 at 15:48

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

It will still return a 200 OK status, it's the output that's messed up, not the response
Likewise, $today=date(l); should be $today=date("l");
And make sure there's nothing after your closing ?>
Just get rid of closing ?>
1

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 ] );

Comments

1

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').

1 Comment

And what if I add: if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ // if the browser has a cached version of this image, send 304 header('Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],true,304); exit; } ?
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.