connection_aborted() 不适用于 ajax 调用

2022-01-24 00:00:00 connection php ajax

已编辑

我有一个 ajax 调用(使用 $.ajax()) 调用以下 php 脚本.

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);
}

持续 40 秒.

如果我关闭触发调用的浏览器窗口,connection_aborted() 仍然返回 false,即使我显式发送了一个字符串并刷新了缓冲区!

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!

请问这里有人回答吗?

推荐答案

你需要添加ignore_user_abort(true);"在 PHP 脚本之上,并在从脚本中回显某些内容后调用ob_flush()"(有关原因,请参见 PHP flush() 手册页).工作示例(概念证明):

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);
}

附:如果连接仍然处于活动状态,则 connection_status() 返回 0,如果连接已关闭,则返回 1.

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

我的错.同时调用 flush() 和 ob_flush() (请阅读 flush() 手册页、上面的链接以及来自 this topic),否则可能不起作用,具体取决于服务器/php 配置.以下代码在 WAMP 上使用 PHP 5.3.8 进行了测试(无需调用 flush()),现在在 Ubuntu 上使用 PHP 5.3.10 进行了测试.需要在 ob_flush() 之前调用 flush().

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.

完整的测试代码:

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);
}

在您调用 index.html 页面并关闭选项卡或整个浏览器后,您应该会在 log.txt 文件中看到下一个信息:

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

相关文章