Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    59
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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?

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    59
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Apr 2011
    Posts
    59
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Bump! Was wondering if anyone else had any ideas.

  • #5
    New to the CF scene
    Join Date
    May 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New Coder
    Join Date
    Apr 2011
    Posts
    59
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •