Solved

PHP Algorithm

Posted on 2013-01-30
2
264 Views
Last Modified: 2013-01-30
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'];

}

Open in new window


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
Comment
Question by:nm_mario
2 Comments
 
LVL 31

Accepted Solution

by:
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

   $visitorip = $_SERVER["REMOTE_ADDR"]

   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 51

Expert Comment

by:Julian Hansen
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
This article discusses how to create an extensible mechanism for linked drop downs.
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.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now