dtq7387 2013-09-06 18:55
浏览 43

当jquery ajax中止时,中止php脚本执行

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
",  FILE_APPEND);
sleep(1);
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test2.log', "1
",  FILE_APPEND);
sleep(1);
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test3.log', "1
",  FILE_APPEND);
sleep(1);
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test4.log', "1
",  FILE_APPEND);
  • 写回答

1条回答 默认 最新

  • douji5329 2013-09-06 19:06
    关注

    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 "
    
    ";
        ob_flush();
        flush();
        exit;
      }
      file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/test' . ($i+1) . '.log', "1
    ", FILE_APPEND);
      sleep(1);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler