2

Alright, I have been trying to work on this issue for days, and have found no fix. I've asked about 4-5 different questions pertaining to my issue, but have found no solution.

file.php on example1.com:

I have a JavaScript function on this page that sends a POST request to a PHP file on example2.com (a DIFFERENT domain).

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

This function dynamically loads the result of runner.php into a div on the page.

Now if the user leaves the page in the middle of execution (the result hasn't been generated yet) and then he/she refreshes the page, and then decides to run the function again, then two PHP proccesses would be running from the same user at the same time (that is, if the old one was still running).

Now I need a fix, whether that be in the PHP file on example2.com or within the JavaScript on example1.com that can abort/stop the previous PHP request before starting a new one.

What I've Tried:

#1

I've tried this, but unfortunately cross-domain cookies is practically impossible:

PHP session files saved, but no cookies and session not read

The goal was to store the PHP pid in a SESSION variable, and on every run, to check if the old proccess with that pid was still running (by grabbing the pid from the SESSION variable), if so, then I would kill that processes and change the value of the SESSION variable to the new pid.

#2

I've also tried to run a loop to check if the connection has been aborted while the main script runs. However, that also did not work:

PHP run loop and script at same time

#3

I also thought about doing something like this:

window.onbeforeunload = function(event) {
    http.abort();
};

So it would abort the request before the user left/refreshed the page/browser. However, I was unsure about the reliability of this (Safe Way to Send POST Request via Javascript).

Community
  • 1
  • 1
user3681788
  • 221
  • 4
  • 15

2 Answers2

0

When You execute Your script runner.php for the first time You can create a file and lock it until script ends, so when there is another request to that script You can check if file is locked - and then You have information if You can run script again, example:

$fo = fopen('lock.txt', 'r+'); 
if (flock($fo, LOCK_EX)) 
{
    // File was not locked, You have locked it successfully right now, do your logic 
    flock($fo, LOCK_UN); // unlock file afer script logic
} 
else
{ 
    // File is locked right now, don't run script
}

After that You can send message to user that he can try later (cause first script is running) instead of killing first script.

I think this is design problem when it comes to killing script, it doesn't look well. What about data when You kill script in middle of its execution? This can lead to some serious issues.

szymanskilukasz
  • 463
  • 3
  • 13
  • However, I want multiple people to be able to use it at once. I just want the script to stop executing when the user leaves the page from which the AJAX request was sent. Plus, the script takes a long time to finish sometimes, and can even not respond. So for that reason I'd rather not put a lock on, but just kill the execution of the script. – user3681788 Jul 23 '14 at 16:31
  • @user3681788 So you can create files with usernames to lock only on unique users, or use some key value store like redis i.e. I think this is design problem when it comes to killing script, it doesn't look well. What about data when You kill script in middle of its execution? This can lead to some serious issues. – szymanskilukasz Jul 23 '14 at 16:35
0

As pointed up by the other user, using files are the best way for doing this. My logic would be something like this:

When the runner.php runs:

  • First check if a file named username+number exists
  • If exists then delete that file, and create a new file named username + (number+1)
  • If does not exist then create a file named username+1
  • Store this number (i.e. 1 or number+1) in a variable
  • Do whatever processing is required
  • Before sending out the response back, check if the file named (username+ your number in var) exists
  • If yes, send out the response
  • If no, it means that another (later) process is running (only for that user because we name the file with username) and that process has deleted your current process's file, so just terminate without doing anything.

Of course this might not be the optimal solution, but should work for your use case.

Check the file functions in PHP, there are no complicated magic code required for this.

raidenace
  • 12,789
  • 1
  • 32
  • 35
  • However, what if, let's say, the user has multiple accounts and on his/her first attempt uses `account1`, but in the middle of execution leaves the page and retries with `account2`. With that being the case, two PHP processes would be running. – user3681788 Jul 23 '14 at 16:40
  • @user3681788 yeah but for different accounts. You must tell us what Your script doing. – szymanskilukasz Jul 23 '14 at 16:42
  • @raidenace Perhaps, but I wouldn't want the process for `account1` to just hang in there and chew up server resources if it were to take too long/practically become unresponsive. – user3681788 Jul 23 '14 at 16:43
  • @szymanskilukasz - I concur. I am thinking that the same runner.php would be doing processing of different datasets for different users. In which case the parallel processing worry should only be there for the same user invoking it twice, not multiple users. – raidenace Jul 23 '14 at 16:43
  • @user3681788: I thought that should be the case. Wouldnt runner.php be processing different datasets for two different users? – raidenace Jul 23 '14 at 16:45
  • @user3681788 This is weird. You script is very resources consuming and You want to multiple people use it at once? You should refactor Your code or redesign Your app logic I think. – szymanskilukasz Jul 23 '14 at 16:47
  • @user3681788: In which case you will need to check if file exists at every step and exit whenever your file gets deleted, which to me is a pretty unstable and bad design. – raidenace Jul 23 '14 at 16:49