Solved

# PHP Algorithm

Posted on 2013-01-30
266 Views
Hello,

I have a table in mysql which all it does is hold a value, a counter, every time someone visits my website, it adds value = value + 1.

Every 51 visitors should see a popup saying that they win something:

``````function is_twelve_pack_winner()
{
global \$custom_conn;
\$sql = "SELECT * from code_win_count LIMIT 1";
\$rs = \$custom_conn->execute(\$sql);
\$count = \$rs->fields['code_wins'];

}
``````

So if 50 people visit the website, visitor number 51 should get a popup saying that they can redeem a coupon... if 100 people visit the website, number 101 will also get a popup saying that they win something and so on and so on...

any ideas?
0
Question by:nm_mario

LVL 31

Accepted Solution

Frosty555 earned 500 total points
ID: 38838109
One consideration you should think about is whether you are counting "page loads", or "unique visitors". You most likely want to popup every 51 unique visitors. That way a user can't just hit refresh 50 times and win the prize. You can count unique visitors by examining their IP address. If they have already visited the page at some point today their page load shouldn't increment the counter.

What I'd suggest is have your MySQL table hold a list of IPs, the number of times they have visited the page, and the last day of the year they were on the page.

For example:

Visitors Table:
visitorid       integer primary key
ip                  varchar
lastvisit        int
visitcount    int

Every time a visitor hits your website, your code does the following:

- Find the record in MySQL for that user's IP

SELECT * FROM Visitors where ip = '\$visitorip'

- If the day of the year is different than lastvisit field, bump the visitcount

\$dayofyear = date('z');
if( \$dayofyear != \$row["lastvisit"] ) {
// update the table
}

Update the table using SQL like this:

UPDATE Visitors set visitcount = visitcount+1,  lastvisit = '\$dayofyear' LIMIT 1

To determine if you should be popping up the popup, you want to sum all of the visit counts together, e.g.

SELECT SUM(visitcount) FROM Visitors

Then check if it is divisible by 51 in PHP, for example:

e.g.

if(    \$totalvisits  % 51   == 0  ) {
// the popup should be shown
}
0

LVL 54

Expert Comment

ID: 38838334
I wouldn't use IP because if you have multiple people behind a firewall they will all have the same IP.

Rather use the session ID or put a cookie on their machine.

Also \$totalvisits % 51 won't give you you the right answer.

This will give you the

51
102
153

Etc

What you actually want is

if (\$totalvists % 50 == 1) {
}
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.