Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1098383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:34:19+00:00 2026-05-17T00:34:19+00:00

We have a web application (it is a game) with lots of various forms

  • 0

We have a web application (it is a game) with lots of various forms and elements which act as buttons and trigger some actions on server. The problem is that users can sometimes confuse our application if he clicks on buttons too fast or opens the website in two tabs and then issues some actions simultaneously. We have some basic protection – MySQL transactions, some double-click preventing Javascripts, but anyway sometimes something just skips out. Of course, the best way would be to redesign all the SQL transactions and supporting functions in the way that does not allow to confuse the system. One example of such confusion is issuing two updates simultaneously – one web request changes something in the db, but the second request operates still with the old data and so SQL update returns “number of affected rows was zero” because the first transaction has already changed data in the db.
The obvious solution is to read data once again right before UPDATE to see if it still needs updating, but that means putting many more double SELECT queries everywhere, not a good solution – why to read the same data from db twice?
Also we have considered using some hidden token to compare on the server on each updating request and deny operations which have the same token id, but this also means touching really many places of code and possibly introducing new bugs into the system which works just fine except this one problem.

The logic of the action flow would be the following: if the user issued two requests simultaneously, the second request should wait until the first completes. But we must also consider that there are many redirects in our application (for example after POSTs to avoid double POSTing when user refreshes the page), so the solution should not create deadlocks for a user.

So the question is:
what would be the most easy possible global fix which could just somehow make all the user operations sequential? Is there any good universal solution?

We are using MySQL and PHP.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:34:19+00:00Added an answer on May 17, 2026 at 12:34 am

    I’m glad you realise that its only a bad and temporary solution and you should optimize your code.
    Forget your tokens and stuff.
    The easiest and still efficient way is probably to have an exclusive file lock on a shared file per operation. You can imagine this like a piece of wood and only one can hold id and only who holds it is allowed to talk or do something.

    <?php
        $fp = fopen("/tmp/only-one-bed-available.txt", "w+");
    
        if (flock($fp, LOCK_EX)) { // do an exclusive lock
    
             // do some very important critical stuff here which must not be interrupted:
             // sleeping.
             sleep(60);
             echo "I now slept 60 seconds";
            flock($fp, LOCK_UN); // release the lock
        } else {
            echo "Couldn't get the lock!";
        }
    
        fclose($fp);
    ?>
    

    If you execute this script 10 times parallel, it will take 10 minutes to finish (because there is only one bed available!) and you will see the echo “I now slept…” every 60 seconds (approximately).

    This serializes ALL executions of this code GLOBALLY.
    This is probably not what you want (you want it on a per user basis, don’t you?)
    I am sure you have something like User-IDs, otherwise use the foreign IP-Adress, and have a unique filename for each user:

    <?php $fp = fopen("/tmp/lock-".$_SERVER["REMOTE_ADDR"].".txt", "w+"); ?>

    You can also define a lock file name for a group of operations. Maby the user can do some stuff parallel but only this operation makes problems? Give it a tag and include it int he lock filename!

    This practice really isn’t that bad and can be used! There are only little ways to do it faster (mysql internals, shared memory segments, serialisation on a higher layer like load balancer…)

    With the solution posted above you can do it in your application which is probably good.
    You can use the same scheme in Mysql as well:
    http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock

    But the PHP implementation is probably easier and better for your use case.

    If you really wanna kick ass there an even more elegant solution,
    just check out this function http://www.php.net/manual/en/function.sem-get.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make this 2-player web game application using Spring MVC. I have
I have a web application that should behave differently for internal users than external
I have a web application written in C# that consumes several internal web services.
I have a web application developed with ASP.net and C# that is running on
I have a web application that is becoming rather large. I want to separate
I have a web application that needs to take a file upload from the
I have a web application using ASP.NET 2.0 and I want to know if
I have this web application that has grown to an unmanageable mess. I want
I have a web application (asp.net) where work items are submitted and allocated to
I have a web application with users and their documents. Each user can have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.