apache 检测在 PHP 中关闭的浏览器连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/86197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:44:03  来源:igfitidea点击:

Detect browser connection closed in PHP

phpapachehttpmod-php

提问by Cheekysoft

Does anyone know if it is possible to detect whether the browser has closed the connection during the execution of a long PHPscript, when using apacheand mod_php?

有谁知道PHP在使用apacheand时是否可以检测浏览器在执行长脚本期间是否关闭了连接mod_php

For example, in Java, the HttpOutputStreamwill throw an exceptionif one attempts to write to it after the browser has closed it -- Or will respond negatively to checkError().

例如,在 中Java,如果在浏览器关闭后尝试对其进行写入,HttpOutputStream则将抛出一个exception- 或者将对 做出否定响应checkError()

采纳答案by Cheekysoft

回答by Willem

In at least PHP4, connection_aborted and connection_status only worked after the script sent any output to the browser (using: flush() | ob_flush()). Also don't expect accurately timed results.

至少在 PHP4 中,connection_aborted 和 connection_status 仅在脚本将任何输出发送到浏览器后才起作用(使用:flush() | ob_flush())。也不要指望准确计时的结果。

It's mostly useful to check if there is still someone waiting on the other side.

检查另一边是否还有人在等待最有用。

回答by Kent Fredric

http://nz.php.net/register-shutdown-function

http://nz.php.net/register-shutdown-function

Probably less complicated if you just want a script to die and handle it when a user terminates.

如果您只想让脚本终止并在用户终止时处理它,那么可能不那么复杂。

( Ie: if it was a lengthy search, this would save you a bunch of operation cycles )

(即:如果这是一个冗长的搜索,这将为您节省一堆操作周期)