Forum Moderators: coopster
Im basically just wondering if anyone knows of a quick way of accomplishing this.
Thanks in advance!
session_start(); // only if you haven't already started the session
// make sure they have tickets in the barrel
if(!isset($_SESSION['tickets']) ¦¦ count($_SESSION['tickets']) < 1){
// put a thousand tickets in the barrel
$_SESSION['tickets'] = range(1,1000);
}
// spin the barrel every time
shuffle($_SESSION['tickets']);
// pull out a ticket
echo 'You drew number '.$_SESSION['tickets'][0];
// check the ticket to see if it's a winner
if($_SESSION['tickets'][0] == 42){
echo ' You win! :)';
} else {
echo ' Sorry, please try again. :(';
}
// throw out the ticket
unset($_SESSION['tickets'][0]);
If you want it to be per 1,000 page views regardless of user (don't forget spiders and downloaders) then you'll need to store the list of numbers in a file or database on your server.
*Don't store large amounts of data in the session as it can slow down your entire site. [webmasterworld.com] Depending on what else you are storing, anything more than a 1,000 numbers probably shouldn't use sessions (and, depending on your situation, even a couple hundred might be too many to store in sessions).