Removing duplicates from random image script

Hi group, I have this small script that uses array_rand to pick a picture out of a directory and display it. The problem is, alot of the time, the very same image is displayed in the next refresh or within the next 5 refreshes for sure. This is out of a 100 images.

What I was hoping is someone could devise a way to not allow the same picture to display within x amount of refreshes, kind of temporarily remove the image from the array.

Here is the code I am using.

[php]

<?php /******************************************************************************* * SETTINGS * * See readme.htm file for further instructions! *******************************************************************************/ /* The default folder with images */ $settings['img_folder'] = 'images/'; /* File types (extensions) to display */ $settings['img_ext'] = array('.jpg','.gif','.png'); /* How to display the images? 0 = print just the image path (for includes), like: images/test.jpg 1 = redirect to the image, when using: */ $settings['display_type'] = 0; /* Allow on-the-fly settings override? 0 = NO, 1 = YES */ $settings['allow_otf'] = 1; /******************************************************************************* * DO NOT EDIT BELOW... * * ...or at least make a backup before you do! *******************************************************************************/ /* Override type? */ if ($settings['allow_otf'] && isset($_GET['type'])) { $type = intval($_GET['type']); } else { $type = $settings['display_type']; } /* Override images folder? */ if ($settings['allow_otf'] && isset($_GET['folder'])) { $folder = htmlspecialchars(trim($_GET['folder'])); if (!is_dir($folder)) { $folder = $settings['img_folder']; } } else { $folder = $settings['img_folder']; } /* Make sure images fodler ends with an '/' */ if (substr($folder,-1) != '/') { $folder.='/'; } /* Get a list of all the image files */ $flist = array(); foreach($settings['img_ext'] as $ext) { $tmp = glob($folder.'*'.$ext); if (is_array($tmp)) { $flist = array_merge($flist,$tmp); } } /* If we have any images choose a random one, otherwise select the "noimg.gif" image */ if (count($flist)) { $src = $flist[array_rand($flist)]; } else { $src = 'noimg.gif'; } /* Output the image according to the selected type */ if ($type) { header('Location:'.$src); exit(); } else { echo $src; } ?>

[/php]

I found an idea by googling but I couldn’t figure out how to apply it to this script. Here is what someone proposed that seemed to have some success for another user.

[php]
$style_class = array(“st_style1”,“st_style2”,“st_style3”,“st_style4”);
$styles = array()
$lastStyle = -1
for($i = 0; $i < 5; $i++) {
while(1==1) {
$newStyle = array_rand($style_class, 1);
if ($lastStyle != $newStyle) {
$lastStyle = $newStyle;
break;
}
}
$div_class = $style_class[$lastStyle]
$styles[] = $div_class
}
[/php]

Thanks if you can help me.

Sponsor our Newsletter | Privacy Policy | Terms of Service