W3docs

connection_aborted()

The connection_aborted() function in PHP is used to check if the client browser has aborted the connection to the web server.

What connection_aborted() Does

The connection_aborted() function tells you whether the client (usually a browser) has disconnected before your script finished sending its response. A client "aborts" when the visitor closes the tab, hits the stop button, navigates away, or loses their network connection.

It takes no arguments and returns an integer:

  • 1 if the connection was aborted by the client,
  • 0 if the connection is still alive.
connection_aborted(): int

In practice the return value is treated as a boolean, so if (connection_aborted()) reads naturally as "if the client has gone away."

Why You Would Use It

For most pages you never need this function — PHP just stops when the client disconnects. It becomes useful for long-running scripts that must do cleanup or bookkeeping even when no one is waiting for the result, for example:

  • A report or export that takes minutes to generate and must commit a partial result or roll back on disconnect.
  • A script that flushes progress to a log and needs to record "the user gave up at step 7."
  • A background-style task triggered by a web request that should keep working after the user leaves.

There is an important catch: PHP cannot detect an aborted connection until it next tries to send output to the client. So connection_aborted() only flips to 1 after you attempt a write (echo/print) and the buffer is flushed. You typically pair it with flush() inside a loop so PHP actually pushes data and notices the disconnect.

By default PHP also kills the script the moment it detects the client has left. To keep running after a disconnect — which is the whole point of checking connection_aborted() yourself — you must first call ignore_user_abort(true).

Basic Example

This script asks PHP to keep running after a disconnect, then loops and checks the connection on each iteration. When the client aborts, it writes a note to a log and stops.

<?php

// Keep executing even if the client disconnects.
ignore_user_abort(true);

for ($i = 0; $i < 60; $i++) {
    // Send something and flush so PHP can detect a closed connection.
    echo "Working on step $i ...\n";
    flush();

    if (connection_aborted()) {
        file_put_contents('job.log', "Client left at step $i\n", FILE_APPEND);
        break;
    }

    sleep(1); // simulate slow work
}

connection_aborted() returns 0 while the browser is still listening and 1 after the visitor closes the tab; the break then ends the loop cleanly.

Distinguishing Why the Script Stopped

A long script can end for two reasons: the client aborted, or it hit the configured time limit. connection_aborted() answers the first; connection_status() answers both at once by returning a bitmask. The following standalone snippet shows the constant values PHP uses, which you can run with the PHP CLI:

<?php

// The bit flags returned by connection_status()
echo "ABORTED  = " . CONNECTION_ABORTED . "\n"; // 1
echo "TIMEOUT  = " . CONNECTION_TIMEOUT . "\n";  // 2
echo "NORMAL   = " . CONNECTION_NORMAL . "\n";   // 0

When run, this prints:

ABORTED  = 1
TIMEOUT  = 2
NORMAL   = 0

So connection_aborted() is effectively shorthand for testing the CONNECTION_ABORTED bit of connection_status().

Common Gotchas

  • It needs output + flush. Without an echo/flush(), the abort is never detected and connection_aborted() stays 0.
  • Output buffering hides aborts. If ob_start() or implicit buffering holds your output, PHP never writes to the socket. Flush the buffers (for example with ob_flush() then flush()).
  • It only works in a web request. Run from the CLI, the function always returns 0 because there is no client to disconnect.
  • You usually want ignore_user_abort(true) first, or the script is terminated before your check ever runs.

Conclusion

connection_aborted() lets a PHP script react when a visitor disconnects mid-request — returning 1 after an aborted connection and 0 while it is still open. It is most valuable in long-running scripts where you pair it with ignore_user_abort() and flush() to finish cleanup work even when no one is waiting for the response.

Practice

Practice
What does the function connection_aborted() in PHP do?
What does the function connection_aborted() in PHP do?
Was this page helpful?