PHP 中检测浏览器连接关闭
有谁知道在使用apache
和mod_php
时是否可以检测浏览器在执行长PHP
脚本期间是否关闭了连接>?
例如,在 Java 中,如果在浏览器关闭它之后尝试写入,HttpOutputStream
将抛出一个异常
- 或者将对 checkError()
做出否定响应。
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()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 connection_aborted()
Use connection_aborted()
至少在 PHP4 中,connection_aborted 和 connection_status 仅在脚本将任何输出发送到浏览器后才起作用(使用:flush() | ob_flush())。
也不要指望准确的时间结果。
检查对方是否还有人在等待最有用。
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.
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 )