Page 1 of 1

Read random image, but from cache if possible

Posted: Thu Jun 23, 2005 2:59 pm
by Skara
I'd like to have a random image on a forum, but it reloads the images whether you've seen them before or not.
Here's the very simple script I've got...

Code: Select all

<?php
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache'); 
header('Content-Type: image/jpeg');
$images = array(
  'three',
  'beenthere',
  'time',
);
$image = $images[array_rand($images)];
readfile('./'.$image.'.jpg');
?>
I seriously doubt this is possible, but just to make sure... Is there a way to have it try and read the cache if the user has seen a particular image or not?

If NOT, then how about this... One random image per day. Once the user has seen the image, it stays the same (reads from cache) until the next day. That possible?

Posted: Thu Jun 23, 2005 3:50 pm
by Revan
I'm not totally sure about cache managment with PHPs header() func, but you could try using mod_expires with mod_headers, if you have Apache.

Posted: Thu Jun 23, 2005 4:08 pm
by John Cartwright
you could set a cookie and check if the cookie exists, along with a timestamp.

Posted: Thu Jun 23, 2005 7:02 pm
by Skara
Ok, the cookie part works fine, but it still reloads the image each time. :/

Code: Select all

<?php
error_reporting(0);
header('Content-Type: image/jpeg');
if (isset($_COOKIE['skara-sig-image'])) {
  $image = $_COOKIE['skara-sig-image'];
}
else {
  $images = array(
    'three',
    'beenthere',
    'time',
  );
  $image = $images[array_rand($images)];
  setcookie('skara-sig-image',$image, time() + 86400);
}
readfile('./'.$image.'.jpg');
?>
What now?

Posted: Thu Jun 23, 2005 7:07 pm
by John Cartwright
Skara wrote:Ok, the cookie part works fine, but it still reloads the image each time. :/
Sorry for being thick, what do you mean

Posted: Thu Jun 23, 2005 7:34 pm
by Skara
Well, with cache the image only loads from the net once, then it's read from your comp. This loads from the net every time.
http://sealionpools.com/storage/randsig.php < just reload it a few times
http://sealionpools.com/storage/beenthere.jpg < then load this one a few times
The first one reloads every time while the second doesn't.

Posted: Thu Jun 23, 2005 7:45 pm
by timvw

Code: Select all

header('Last-Modified: ' . $pubdate);
header('ETag: "' . $pubdate . '"');
A websearch on the following headers will provide you enough information to come up with a solution ;)

Posted: Thu Jun 23, 2005 9:16 pm
by Skara
alright, the last-modified got me all fixed up. ^_^ thanks.

Could you explain etag, though? I can't seem to figure out anything about it except something about md5'ing it.

Posted: Thu Jun 23, 2005 9:34 pm
by John Cartwright

Posted: Thu Jun 23, 2005 10:40 pm
by Skara
Ok, I get what it does.. Someone want to give me an example of how to use it, please?

Posted: Fri Jun 24, 2005 2:12 am
by timvw

Posted: Fri Jun 24, 2005 1:02 pm
by Skara
ah, thank you. very helpful. ;)