SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dealing with Lag and Mysql

    Im having a hard time trying to find a solution to this problem. Basically the lag in a script is causing alot of problems. Obviously the first solution would be to get rid of the lag. Lets say in this case the lag cannot be eliminated. So heres an example.

    I have a battle script which attacks another player and subtracts one of the battles you have left. Now if this script takes a long time to load and you click 10 different links simulataniously. This means its loading 10 versions of the script and in each one it checks to see if you have at least 1 battle left. While its executing the battle and by the time it subtracts 1 all those scripts have ran and your now left with -5 if you had 5 battles to begin with.

    So in theory, if you have a script that lags and that people open it multiple times, how do you prevent them getting away with more than they are supposed to since the code executes before it subtracts. Like it runs the 10 select statements before it runs the update statement once so it would never enter that if statement.

    The script would look something like this:

    Code PHP:
    $player = mysql_fetch_array(mysql_query("select battlesleft from table"));
     
    if($player['battlesleft'] < 1) {
    echo "you have no battles";
    exit;
    }
     
    mysql_query("Update table set battlesleft=battlesleft-1 where id='$player[id]'");

    Even if the lag is eliminated in theory if you can click fast enough you could get away with more things then you are supposed to?

    Does anyone have any ideas how to fix this issue? Thank you for any advice you might have.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Is that mysql query the one that you actually use?

    If so, then you need to add a WHERE clause to it, otherwise it returns all of the data in the table, causing the lag.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No that was just a quick example I through together to demonstrate.

    The actual script is several thousand lines long and does alot more processing.

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One thought would be to generate a one-time-use token for each player on page load. Then on your long-processing script, the first thing you do is invalidate that token (thus preventing your user from spamming the "do battle" link), then do your processing, and at the end generate a new token for the user. Or maybe easier would be to set a is_processing flag in their session:
    PHP Code:
    //user clicked on "do battle", so do battle!
    ignore_user_abort(true); //we want to keep processing even if they abort
    //first, is a battle already being processed?
    if($_SESSION['is_processing'] == true)
    {
        
    header("Location: http://yourdomain.com/pleasewait.php");
        die;
    } else {
        
    $_SESSION['is_processing'] = true;
    }

    //now do your processing here

    //processing all done
    $_SESSION['is_processing'] = false
    So now if someone clicks on "do battle", then clicks on it a second time while the first script is still processing, they get sent to a "please wait" page that tells them you're still processing their first battle and they need to be patient. The ignore_user_abort(true); line will make sure that the first battle continues to process regardless of how many times they click other links or spam their browser's refresh button etc.

    Test this thoroughly before you put it into production, however, because a simple goof that prevents "unsetting" the is_processing flag will lock out your users!
    PHP questions? RTFM
    MySQL questions? RTFM


Bookmarks

Posting Permissions

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