在 PHP 中启用输出缓冲来检测用户中止

发布于 2024-10-21 07:18:23 字数 935 浏览 1 评论 0原文

ignore_user_abort()函数文档中的注释部分表明 PHP 无法检测到用户已中止请求,如果没有数据发送到客户端。对于函数 connection_status()< 来说,情况确实如此。 /a>,也是。不幸的是,我需要在使用输出缓冲的页面上检测用户中止(可以取消的ajax请求)(并且我不能轻易更改它)。

除了使用该函数之外,还有其他方法可以检测用户中止吗 connection_status()?或者有什么具体方法可以使该函数返回正确的值? PHP 实际上应该知道请求已中止,因为从客户端收到了 FIN 数据包。

我已经尝试分析流元数据 php://inputphp://outputphp://stdinphp://stdout > 在连接被中止之前和之后以阻塞和非阻塞方式读取/写入数据之后,但这没有提供任何有用的状态更改。

The Notes section in the function documentation of ignore_user_abort() suggest that PHP cannot detect that a user has aborted the request if no data is sent to the client. This is true for the function connection_status(), too. Unfortunately, I need to detect a user abort (an ajax request that can be cancelled) on a page which makes use of output buffering (and I cannot change that easily.)

Is there any other way of detecting a user abort except using the function
connection_status()? Or any specific way to make that function return the correct value? PHP should actually know that the request was aborted, since a FIN packet is received from the client.

I have already tried analyzing the stream metadata of php://input, php://output, php://stdin and php://stdout after reading/writing data in a blocking and non-blocking manner before and after the connection was aborted, but that didn't provide any useful state changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送君千里 2024-10-28 07:18:23

愚蠢的建议,但是......您是否尝试过向客户端发送一些数据,然后在开始输出缓冲之前发送flush()?我能想到的唯一其他解决方案是转义缓冲区,但我可以想象它会非常麻烦,正如您所说。

也许是一个帮助破坏缓冲区的助手......

function OBWanCallback($buffer)
{
    if( OBWan::$isFinished )
    {
        // -- Actual callbacks go here ...
    }

   return $buffer;
}

OBWan::startbuffer('OBWanCallback');
[ // -- Example functionality
    self::$callback = $callback;
    ob_start(self::$callback);
]

// -- in some code far, far away ...

OBWan::suspendbuffer();
[ // -- Example functionality
    self::$buffercache = ob_get_clean();
]

echo " ";
flush();

OBWan::resumebuffer();
[ // -- Example functionality
    ob_start(self::$callback);
    echo self::$buffercache;
    self::$buffercache = "";
]

// -- in some code far, far away ...

OBWan::outputbuffer();
[
    self::$isFinished = true;
    return ob_get_clean();
]

如果您已经实现了深度,则可以用一些东西来解释您已实现的缓冲区的深度。

Silly suggestion, but ... have you tried sending some data to the client, followed by a flush() before you start the output buffering? The only other solution I can think of is to escape the buffer(s), but I can imagine how it could be quite troublesome, as you said.

Maybe a helper to assist with breaking the buffer ...

function OBWanCallback($buffer)
{
    if( OBWan::$isFinished )
    {
        // -- Actual callbacks go here ...
    }

   return $buffer;
}

OBWan::startbuffer('OBWanCallback');
[ // -- Example functionality
    self::$callback = $callback;
    ob_start(self::$callback);
]

// -- in some code far, far away ...

OBWan::suspendbuffer();
[ // -- Example functionality
    self::$buffercache = ob_get_clean();
]

echo " ";
flush();

OBWan::resumebuffer();
[ // -- Example functionality
    ob_start(self::$callback);
    echo self::$buffercache;
    self::$buffercache = "";
]

// -- in some code far, far away ...

OBWan::outputbuffer();
[
    self::$isFinished = true;
    return ob_get_clean();
]

with something to account for the depth of buffers you've implemented, if you have implemented depth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文