dongshan9338 2012-07-24 09:27 采纳率: 0%
浏览 215
已采纳

connection_aborted()不适用于ajax调用

EDITED

I've got an ajax call (using $.ajax()) which calls the following php script.

for ($i=0;$i<40;$i++) {
    echo " ";
    flush();
    if (connection_aborted()) {
        log_message('error','CONNECTION IS ABORTED!!!!!');
        exit;
    }
    else {
        log_message('error','connection not aborted :(');
    }
    sleep(1);
}

This goes for 40 Seconds.

If I close the browser window which triggered the call, connection_aborted() still returns false, even if I sent explicitly a string and flushed the buffer!

Does anyone have an answer here please?

  • 写回答

1条回答 默认 最新

  • doudu9148 2012-08-20 22:23
    关注

    You will need to add "ignore_user_abort(true);" on top of the PHP script, and to call "ob_flush()" after echoing something from script (For why see PHP flush() man page). Working example (proof of concept):

    <?php
    
    ignore_user_abort(true);
    
    function log_message($s, $ss) {
      $myFile = "log.txt";
      $fh = fopen($myFile, 'a') or die("can't open file");
      $stringData = $s . ": " . $ss . "
    ";
      fwrite($fh, $stringData);
      fclose($fh);
    }
    
    
    
    for ($i=0;$i<5;$i++) {
    
        echo "<br>";
        //flush();
        ob_flush();
    
        if (connection_aborted()) {
            log_message('error1', connection_status());
            exit;
        }
        else {
            log_message('error2', connection_status());
        }
    
        sleep(1);
    }
    

    P.S. connection_status() returns 0 if connection is still active, and in case of closed one returns 1.

    EDIT:

    My bad. Call both flush() and ob_flush() (please read flush() man page, link above, and answers from this topic), or otherwise might not work, depending on server/php configuration. The following code was tested on WAMP with PHP 5.3.8 (works without calling flush()), and now on Ubuntu with PHP 5.3.10. where flush() call before ob_flush() is necessary.

    Full code for testing:

    index.html:

     <html>
      <head>
        <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
    
        <script>
    
          $(document).ready(function() {
    
            $.ajax({
              url: "script.php",
              context: document.body
            }).done(function(data) { 
             alert(data);
            });
    
          })
    
        </script>
    
      </head>
    
      <body>
      </body>
    
    </html>
    

    script.php:

    ignore_user_abort(true);
    
    function log_message($type, $message, $file = 'log.txt') {
        $fh = fopen($file, 'a') or die("can't open file");
    
        $conn_status = connection_status();
    
        if($conn_status === CONNECTION_NORMAL) {
            $status = 'normal';
        } elseif($conn_status  === CONNECTION_ABORTED) {
             $status = 'aborted';
        } else {
            $status = 'timeout';
        }
    
        $aborted = connection_aborted() ? 'yes' : 'no';
    
        $data  = $type . ': ' . $message . "
    ";
        $data .= 'Connection status: ' . $status . "
    ";
        $data .= 'Aborted: ' . $aborted . "
    
    
    ";
    
        fwrite($fh, $data);
        fclose($fh);
    }
    
    
    
    for ($i = 0; $i < 10; $i++) {
    
        echo "<br>";
        flush();
        ob_flush();
    
        if (connection_aborted()) {
            log_message('Error', 'Connection closed by user!');
            exit;
        }
        else {
            log_message('Info', 'Everything is fine. Move along...');
        }
    
        sleep(1);
    }
    

    After you call index.html page, and close tab or whole browser you should see in log.txt file next info:

    Info: Everything is fine. Move along...
    Connection status: normal
    Aborted: no
    
    
    Info: Everything is fine. Move along...
    Connection status: normal
    Aborted: no
    
    
    Info: Everything is fine. Move along...
    Connection status: normal
    Aborted: no
    
    
    Error: Connection closed by user!
    Connection status: aborted
    Aborted: yes
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA