Solved

PHP Algorithm

Posted on 2013-01-30
2
265 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 52

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 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

16 Experts available now in Live!

Get 1:1 Help Now