Detect browser connection closed in PHP

2019-02-17 10:48发布

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

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

3条回答
贪生不怕死
2楼-- · 2019-02-17 11:21

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.

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

查看更多
smile是对你的礼貌
4楼-- · 2019-02-17 11:37

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 )

查看更多
登录 后发表回答