PHP Function Reference

PHP connection_aborted() Function



The PHP connection_aborted() function is used to check whether the client disconnected.

Syntax

connection_aborted()

Parameters

No parameter is required.

Return Value

Returns 1 if client disconnected, 0 otherwise.

Example: connection_aborted() example

In the example below a shutdown function is created which logs an error message if the client is disconnected.

<?php
//defining a shutdown function
function shutdown() {
  if(connection_aborted()) {
    error_log ("Script ".__FILE__." was aborted by the user.");
  }
}

//registering the shutdown function
//it is called when the script ends
register_shutdown_function('shutdown');

//displaying some message
echo "Hello World!\n";
?>

The output of the above code will be similar to:

Hello World!

❮ PHP Miscellaneous Reference