PHP session synchronization

I am developing WebApp in PHP. The user will be able to click the button, and the PHP code will eventually invoke the system exec. Since WebApp will use AJAX, there is a chance that the user may double-click the button or even click another button that will start another exec process.

Now I know that I can write a little javascript that would disable the buttons until one event is complete. However, this is client-side enforcement, which can be easily overwritten.

Is there something that I could do on the PHP side to stop things like this from happening? In my head, I have the idea that a session variable acts like a "semaphore", and before each script is executed, this variable will be checked and should return 0 before the script can continue.

Am I right about this? Or does it just open a can of worms for dead ends / race conditions?

thank

Update: In order to incorporate it into the context, the system I am developing will be used to start, stop, and recreate virtual private servers. If the reimaging button has been clicked, the PHP script calls several bash scripts to start redrawing the VPS. However, if the user tried to start VPS when it was done ....

+5
source share
3 answers

You can use jQuery.ajax()and set the parameter asyncto false:

async Default: true By default, all requests are sent asynchronously (i.e. by default, this value is true). If you need synchronous requests, set this parameter to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Please note that synchronous requests may temporarily block the browser, disabling any actions while the request is active

You can use it as follows:

$.ajax({
    type: "POST",
    url: "file.php",
    async: false,
    data: data,
    success: function(return){
        alert("Success: "+return);
    }
});

If you want to add a bootloader, just apply it as:

startLoader();
$.ajax({
    type: "POST",
    url: "file.php",
    async: false,
    data: data,
    success: function(return){
        alert("Success: "+return);
    }
});
endLoader();

, PHP-, . script. : ?

, : processes . 2 : : process_id; : process_status. - , sha1 (IMAGE). : 1 "", 0 "".

- :

SELECT process_status FROM vps_processes WHERE process_id = sha1(CURRENT_IMAGE);

, 1 0. 1, script; 0, :

UPDATE vps_processes SET process_status = 1 WHERE process_id = sha1(CURRENT_IMAGE);

, , script:

UPDATE vps_processes SET process_status = 0 WHERE process_id = sha1(CURRENT_IMAGE);
+1

, , , , . , , , . IP- . , , IP, .

0
0

All Articles