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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Cheekysoft
  • 35,194
  • 20
  • 73
  • 86

3 Answers3

7

Use connection_aborted()

  • It doesn't work in Fedora22, PHP 5.6.12 even when outputing data, and ignore_user_abort(false). – sivann Oct 29 '15 at 08:33
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.

Willem
  • 918
  • 5
  • 10
  • `It's mostly useful to check if there is still someone waiting on the other side.`: how? – cYrus Jul 15 '11 at 16:32
  • 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. – Willem Jul 18 '11 at 20:51
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 )

Kent Fredric
  • 56,416
  • 14
  • 107
  • 150