Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-26-2012, 03:09 AM   PM User | #1
Kurisvo
New Coder

 
Join Date: Apr 2011
Posts: 59
Thanks: 8
Thanked 0 Times in 0 Posts
Kurisvo is an unknown quantity at this point
Sorting through multiple page requests

So I'm coding a feature where multiple users can click a link. Whoever clicks the link first gets a prize. The problem is when multiple users click the link at the same time - or close enough to the same time that it awards the prize to both users.

I was wondering if there was a way in PHP to compile a list of users requesting the page and then sort it in the order they clicked the link and pull the first? I want to avoid databasing because a) there are so many links that it would really task the system and b) in my experience, if a user clicks it quickly enough it still gives the prize.
Kurisvo is offline   Reply With Quote
Old 06-26-2012, 03:49 AM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by Kurisvo View Post
I want to avoid databasing because a) there are so many links that it would really task the system and b) in my experience, if a user clicks it quickly enough it still gives the prize.
How are you tracking that a link had been clicked currently?
Inigoesdr is offline   Reply With Quote
Old 06-26-2012, 04:37 AM   PM User | #3
Kurisvo
New Coder

 
Join Date: Apr 2011
Posts: 59
Thanks: 8
Thanked 0 Times in 0 Posts
Kurisvo is an unknown quantity at this point
At the moment I'm recording won prizes in a database. I then check the unique hash id of the prize to that table. If the number of rows found is small then 1 that tells me that no one has claimed it. Which works until two people click it near the same time.
Kurisvo is offline   Reply With Quote
Old 06-28-2012, 12:02 AM   PM User | #4
Kurisvo
New Coder

 
Join Date: Apr 2011
Posts: 59
Thanks: 8
Thanked 0 Times in 0 Posts
Kurisvo is an unknown quantity at this point
Bump! Was wondering if anyone else had any ideas.
Kurisvo is offline   Reply With Quote
Old 06-28-2012, 12:19 AM   PM User | #5
patsfans
New to the CF scene

 
Join Date: May 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
patsfans is an unknown quantity at this point
Quote:
Originally Posted by Kurisvo View Post
Bump! Was wondering if anyone else had any ideas.
Short of creating active sessions and tracking them that way, a database would make the most sense primarily because you could include a timestamp field which would track the click down to the second. Otherwise I'm with Inigoesdr in that I'd like to get a better idea of how you're currently doing it in order to give you a better suggestion.
patsfans is offline   Reply With Quote
Old 06-29-2012, 04:34 AM   PM User | #6
Kurisvo
New Coder

 
Join Date: Apr 2011
Posts: 59
Thanks: 8
Thanked 0 Times in 0 Posts
Kurisvo is an unknown quantity at this point
Basically how it happens is there is a prize database table. let's say it looks like this:

Code:
prize_id |  prize_amt

DsdjD    | 10
osdjk     | 12
There's another table called prize_user_claimed

Code:
prize_id | user_id
When a user clicks the claim link for the first one, it looks like: claim.php?id=DsdjD

When that happens I query the first table for a row with prize_id matching the ID in the url if mysqli_num_rows < 1 (since I delete prizes from the first table when they are claimed) then that means it has already been taken. So I print out the message

"This prize has already been claimed!"

If it hasn't been claimed, I add a record to the second table with the prize hash (DsdjD in this case) and delete the record in the first table.

This works perfectly, but in times of high user traffic if two users click the link very closely to each other, they both get it. This wouldn't be much of a problem if I was using a number ID with AI, but the site really needs to use the unique hashes.

What would be ideal is if there could be a way to see all the users trying to access the page and then sort it in the order they clicked the link. And then to be able to interact with that info in PHP.

Any ideas on how this could work? I can't really lock the table because I still want the other links to be active... and I reaaaaally don't want to get rid of the hashes.
Kurisvo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:29 PM.


Advertisement
Log in to turn off these ads.