PeterPopper Posted April 9, 2010 Share Posted April 9, 2010 I want to count visits to a page, and for every 200th visitor echo "You are a winner." What is the best way to do this? Thanks. I am willing to use a simple MySQL database, if necessary. Quote Link to comment https://forums.phpfreaks.com/topic/198061-simple-counter/ Share on other sites More sharing options...
oni-kun Posted April 9, 2010 Share Posted April 9, 2010 There are many counter examples out there. You best just look at them. As for the "winnner" code, You can have a simple IF/ELSE statement on the code that will propagate the visit to the database. But as obvious, Record IP's per visits, so no one person can refresh the page 200 times and get it. Quote Link to comment https://forums.phpfreaks.com/topic/198061-simple-counter/#findComment-1039230 Share on other sites More sharing options...
YourNameHere Posted April 9, 2010 Share Posted April 9, 2010 It's best to use a database and at the bottom of the page... $counter ="value_from_the_DB"; if ($counter % 200) { echo "You're a winner!!!"; } else { $counter++; } //logic to update the database with the new $counter value. Quote Link to comment https://forums.phpfreaks.com/topic/198061-simple-counter/#findComment-1039232 Share on other sites More sharing options...
oni-kun Posted April 9, 2010 Share Posted April 9, 2010 if ($counter % 200) 1) You'll never get definitive results with %200; 200 && 400 %200 == 0. 2) Counter will ALWAYS be 200 and will never update as you're not iterating the counter after the fact. Everyone is a winner now like in those ads? Quote Link to comment https://forums.phpfreaks.com/topic/198061-simple-counter/#findComment-1039237 Share on other sites More sharing options...
PeterPopper Posted April 9, 2010 Author Share Posted April 9, 2010 oni-kun, what would be your solution, since you noted that YourNameHere suggestion will not work. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/198061-simple-counter/#findComment-1039241 Share on other sites More sharing options...
YourNameHere Posted April 9, 2010 Share Posted April 9, 2010 It's best to use a database and at the bottom of the page... $counter ="value_from_the_DB"; if ($counter == 199) { echo "You're a winner!!!"; $counter = 1; } else { $counter++; //logic to update the database with the new $counter value. } That should work then. Quote Link to comment https://forums.phpfreaks.com/topic/198061-simple-counter/#findComment-1039242 Share on other sites More sharing options...
andrewgauger Posted April 9, 2010 Share Posted April 9, 2010 CREATE TABLE ips (id auto_increment primary key, ip char(32)) ALTER TABLE ips ADD UNIQUE ip //the 32 characters is for ipv6 $ip=getenv(REMOTE_ADDR) INSERT IGNORE INTO ips (ip) VALUE ($ip) Now you can: SELECT id ORDER by id desc limit 1; to get the current count. Do the insert statement first, that way if they are a unique visitor, they will increment the value, Now, you need a table that will define which winners are given away. Here is the scneario you need to think of: visitor 200 just collected his prize and I visit the site but am not a unique visitor, I get a prize too because of the %200 issue. So have a table that If winner, you query to see if the prize is given away (so the table will have something like counter, email and counter would get 200, 400, etc but you need to update the table AS SOON as you determine winner, not after you collect email info. Quote Link to comment https://forums.phpfreaks.com/topic/198061-simple-counter/#findComment-1039307 Share on other sites More sharing options...
PeterPopper Posted April 9, 2010 Author Share Posted April 9, 2010 Thanks very much, YourNameHere and andrewgauger. Quote Link to comment https://forums.phpfreaks.com/topic/198061-simple-counter/#findComment-1039409 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.