I'm looking to stop the execution of a PHP script when the jQuery $.post
is aborted by abort()
. Basically whenever a user changes any input field the $.post
should hit the test.php
. If a second input is changed before the first request returns, the $.post
is aborted and a new one is sent. I want PHP to stop execution when this happens.
According to PHP manual on connection handlnig :
connection_status() return ABORTED state ONLY if the client disconnects gracefully (with STOP button). In this case the browser send the RST TCP packet that notify PHP the connection is closed.
Is there anyway to send the RST TCP
when using xhr.abort()
?
To make sure I've tested this in the test.php
file. Theoretically if the script is stopped the files should have different sizes and number of lines in them. But they're all the same. So the script is not aborting.
jQuery main file:
var xhr = false;
$('input').on('change', function(){
if(xhr){
xhr.abort();
}
xhr = $.post('/test.php', this.value, function(b64){
xhr = false;
});
});
PHP test.php
ignore_user_abort(false);
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test1.log', "1\r\n", FILE_APPEND);
sleep(1);
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test2.log', "1\r\n", FILE_APPEND);
sleep(1);
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test3.log', "1\r\n", FILE_APPEND);
sleep(1);
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test4.log', "1\r\n", FILE_APPEND);