unsigned INT explodes...

Discussion in 'PHP' started by x0x, Jan 16, 2009.

  1. #1
    I have a problem.On my site my users have some resources to do things.

    Let's say the resources field is called 'moves'

    If they go on a certain page, they get some moves deducted from them.

    The problem is tht if they go on that page and refresh it very fast and at the same time their 'moves' run out (insufficient funds), the page will still try to deduct the moves. Once that happens the 'moves' of that user explode to 4 trillion.

    The page has got limits that shouldn't make that possible (if cost of moves > moves, then it won't load). But if they click the button or refresh the page 10 times per second the page will skip that error part and still try to deduct the moves...

    My question is what can I do to stop the field from exploding? Could it go to 0 instead of 4 trllion? Taking out the ability to refresh or click the button many times per second is out of the question.

    Suggestions?
     
    x0x, Jan 16, 2009 IP
  2. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    how do you store 'moves' for users? in db or somehow on client side (browser)?
    anyway it soundas as overflow.

    see warning section on
    www.php.net/int

    think you can workaround this by comparing balance and amount to deduct before real deduction
     
    cont911, Jan 16, 2009 IP
  3. artiskool

    artiskool Peon

    Messages:
    34
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try checking the client's connection first before deducting moves using the connection_aborted() function in PHP

    
    if (!connection_aborted()) {
        // code goes here to deduct user moves
    }
    
    PHP:
     
    artiskool, Jan 16, 2009 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    mysql > ALTER TABLE table MODIFY movies INT UNSIGNED NOT NULL;
    This will not allow negative values in the db column.
     
    Kaizoku, Jan 17, 2009 IP
  5. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5


    The moves are stored in the database.



    But that can cause problems as well...



    connection aborted = when they don't let the page load entirely? That qualifies as an abort?
     
    x0x, Jan 17, 2009 IP