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).