0

I'm currently sending POST requests to a PHP file of mine via a button with the following function:

function buttonFunction() {
    $.post("http://ipaddress/core/file.php",{username:username, password:pword, coins:coins}, function(data) {
        // Stuff
    });
}

However, I recently found out that if the file process/PHP script is still running (trying to obtain the resulting data/response), and the user refreshes the page, the PHP process would still be running on the server. Also, if the user then decided to click the button again (after refreshing), there would be TWO PHP proccesses running from the same user on the server (that is, if the first one was still running):

Javascript Abort POST Request on Retry/Refresh (HIGH CPU Usage)

However, I came across sending POST data with XMLHttpRequest with Javascript:

Send POST data using XMLHttpRequest

So let's say I send my POST request this way, would it be safe to say when the user refreshes/closes out of the page, the PHP execution ends?

function buttonFunction() {
    var http = new XMLHttpRequest();
    var url = "get_data.php";
    var params = "lorem=ipsum&name=binny";
    http.open("POST", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");

    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(params);
}

However, if this also does not work, how can I fix this issue (multiple scripts running in the background from the same user)? Whether that fix be in the PHP file or in the JavaScript itself, any help would be appreciated.

Edit 1:

Possible Solution?

What if I use XMLHttpRequest and abort the request before the page unloads?

window.onbeforeunload = function(event) {
    http.abort();
};
Community
  • 1
  • 1
user3681788
  • 221
  • 3
  • 15
  • 1
    No, if you POST to a PHP file this way, it will still run until completion or server timeout. Whether you POST to it directly or through AJAX doesn't matter. – Ben Dyer Jul 22 '14 at 17:53
  • By the way, if you can't architecturally change your application to run faster, and the user absolutely *must* wait on your script to finish, use AJAX, but provide them with some status of the operation. Without any UI hints that something is happening, users will be more likely to think it's not working and try submitting again. – Ben Dyer Jul 22 '14 at 17:55
  • @BenDyer There are some instances where the code actually might not work (and continue to load), for which reason the user would be forced to refresh no matter what. With that being the case, if there a way I can fix this issue? – user3681788 Jul 22 '14 at 17:57
  • If that's the case, then you should use try/catch in your PHP script to detect when the bad situation occurs and then display that back to the user. In other words, the PHP script should output "Operation successful!" or "Operation failed: Here Is Why" or whatever. Then in your Javascript, read the value of what is returned and when an error is detected, explain what happened and what the user needs to do. – Ben Dyer Jul 22 '14 at 18:01
  • @BenDyer However, my application is very complex and works with sending/establishing/closing packets/sockets and things of that nature. Sometimes the connections are such that just don't respond. – user3681788 Jul 22 '14 at 18:08
  • You probably want to look into http://php.net/manual/en/function.ignore-user-abort.php – Mike Jul 22 '14 at 18:11
  • @Mike Thank you. However, does it look like my possible solution can work? – user3681788 Jul 22 '14 at 18:25
  • @user3681788 Give it a try and see what happens. – Mike Jul 22 '14 at 18:34

0 Answers0