Detect browser connection closed in PHP
Asked Answered
H

3

7

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().

Hexad answered 17/9, 2008 at 18:35 Comment(0)
A
7

Use connection_aborted()

Aero answered 17/9, 2008 at 18:42 Comment(1)
It doesn't work in Fedora22, PHP 5.6.12 even when outputing data, and ignore_user_abort(false).Tucker
C
2

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.

Cerellia answered 17/9, 2008 at 20:12 Comment(2)
It's mostly useful to check if there is still someone waiting on the other side.: how?Scuff
How: "echo" something or output a header(), call 'flush()' and then use you can get connection_aborted() to determine if the HTTP-client is still 'listening' or maybe timed out.Cerellia
M
1

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 )

Mandle answered 17/9, 2008 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.