中止的PHP脚本执行时,jQuery的阿贾克斯被中止 [英] Abort php script execution when jquery ajax is aborted
问题描述
我在找停止PHP脚本执行的时候了jQuery $。交
是中止中止()
。基本上只要用户更改任何输入字段的 $。交
应该打的的test.php
。如果第二个输入端的第一个请求返回之前改变了, $。交
中止,一个新的发送。我想PHP停止执行时发生这种情况。
据上连接 PHP手册handlnig :
CONNECTION_STATUS()返回ABORTED状态,只有在客户端断开优雅(带有STOP按钮)。在这种情况下,浏览器发送通知PHP连接关闭的RST TCP数据包。
反正是有送 RST TCP
使用 xhr.abort()的时候
?
要确保我在的的test.php
文件测试这一点。理论上,如果脚本被停止的文件应该具有不同的尺寸和在其中行数。但他们都是一样的。因此,脚本不中止。
jQuery的主文件:
VAR XHR = FALSE;
$('输入')。在('改变',函数(){
如果(XHR){
xhr.abort();
}
XHR = $。员额('/ test.php的',THIS.VALUE,功能(B64){
XHR = FALSE;
});
});
PHP test.php的
ignore_user_abort(假);
file_put_contents($ _ SERVER ['DOCUMENT_ROOT']'/测试/ test1.log,1 \ r \ N,FILE_APPEND。);
睡眠(1);
file_put_contents($ _ SERVER ['DOCUMENT_ROOT']'/测试/ test2.log,1 \ r \ N,FILE_APPEND。);
睡眠(1);
file_put_contents($ _ SERVER ['DOCUMENT_ROOT']'/测试/ test3.log,1 \ r \ N,FILE_APPEND。);
睡眠(1);
file_put_contents($ _ SERVER ['DOCUMENT_ROOT']'/测试/ test4.log,1 \ r \ N,FILE_APPEND。);
这可以帮助:
注
PHP将无法检测到用户已经中止的连接直到试图将信息发送到客户端。只需用echo语句不保证该信息发送,请参阅的flush()一>
和也: http://www.php.net/manual /en/function.connection-aborted.php
也许有点重写将帮助:
ignore_user_abort(假);
为($ i = 0; $ I&4;; $ I ++){
使用ob_flush();
冲洗();
如果(connection_aborted()){
回声\ r \ñ\ r \ N的;
使用ob_flush();
冲洗();
出口;
}
file_put_contents($ _ SERVER ['DOCUMENT_ROOT']'/测试/测试($ I + 1).LOG,1 \ r \ N,FILE_APPEND。);
睡眠(1);
}
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 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);
This may help:
Notes
PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client. Simply using an echo statement does not guarantee that information is sent, see flush().
And also: http://www.php.net/manual/en/function.connection-aborted.php
Maybe a little re-write would help:
ignore_user_abort(false);
for($i = 0; $i < 4; $i++) {
ob_flush();
flush();
if(connection_aborted()) {
echo "\r\n\r\n";
ob_flush();
flush();
exit;
}
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test' . ($i+1) . '.log', "1\r\n", FILE_APPEND);
sleep(1);
}
这篇关于中止的PHP脚本执行时,jQuery的阿贾克斯被中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!