Read random image, but from cache if possible

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Read random image, but from cache if possible

Post 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?
Revan
Forum Commoner
Posts: 83
Joined: Fri Jul 02, 2004 12:37 am
Location: New Mexico, USA
Contact:

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you could set a cookie and check if the cookie exists, along with a timestamp.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 ;)
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Ok, I get what it does.. Someone want to give me an example of how to use it, please?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

ah, thank you. very helpful. ;)
Post Reply