random image

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
norm188
Forum Newbie
Posts: 8
Joined: Sun Mar 23, 2003 10:14 pm

random image

Post by norm188 »

Hi,
I'm using the following code to display a random image and link from a file named imageandlinks.php
<?
$array = file("imageandlinks.php");
shuffle($array);
for ($i=0; $i<1; $i++) {
echo $array[$i];
}
?>

This code displays one image and link from the imageandlinks.php file randomly.( each time the page is loaded / or refreshed)

I would like to modify this code for it to display a random image and link from the imageandlinks.php file for 1 day ( or 24 hours) and set a cookie for 30 days for each image that have been displayed.

So if a visitor comes back the same day he/she will see the same picture (also if you click refresh). the visitor should see a new image daily but the cookie should avoid the visitor to see the same image twice within the 30 days.

I appreciate your help.
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

not going to give you all the code but can point you in a general direction.
One method would be to:
[php_man]serialize[/php_man] an array (containing the images displayed to that viewer using the current date as the array's index)
set a cookie ([php_man]setcookie[/php_man]) containing the serialized data (meaning: use the serialized data as the value of the cookie)
when the page loads [php_man]unserialize[/php_man] the cookie data and check against that to see what image (if any) they have viewed for the current day.

That will prevent loading new images too early and prevent duplicate images at the same time.

an alternative to that would be to use a mysql backend.
eg, set a cookie with a unique value that expires in 30 days, query the db for the images that have been viewed ( and the dates that the images have been viewed on ).
Post Reply