Solved

PHP Algorithm

Posted on 2013-01-30
2
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 58

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

623 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