Can closing the browser terminate the PHP script on the server?

PHP won’t notice that the browser closed the connection unless it tries to output something (e.g. echo). If it fails to output something, the script will be terminated unless ignore_user_abort is On.

So the script will terminate only if the following conditions are met:

  • the script attempts to output something (because the script won’t notice that the user aborted the connection until then)
  • AND php’s ignore_user_abort setting is off (if the setting is off, php will terminate the script if fails to output something)

You can avoid the script from terminating by enabling ignore_user_abort.

You can use connection_aborted() at anytime to check is the browser aborted the request.

A script can terminate unexpectedly for other reasons (e.g. max execution time, exception, etc); so you should make use of transactions, so that half-finished changes are canceled if the script terminates abnormally.

Leave a Comment