25

Say a user clicked a button, which resulted in a jquery ajax request being sent to my server.

The server begins a complicated process in response to the ajax request. Lets say that process takes 5 minutes to finish.

In the meantime the user gets bored and closes his browser window.

Will the script on the server continue its processing until it finishes on its normal time, or will it stop?

Same thing if the user visits a url, e.g example.com/process.php?data=xxx. This starts process.php which begins to process the data, and will take 5 mins to finish processing.

Will this processing always continue on, or if the user closes the browser, will it stop?

(I'm asking because I'm concerned about the process being left half finished and resulting in corrupt data).

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Ali
  • 261,656
  • 265
  • 575
  • 769

4 Answers4

34

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.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • You mean if both conditions occur, or if any of them occurs? – Ali Jul 06 '12 at 10:21
  • `PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client. Simply using an echo statement does not guarantee that information is sent, see flush().` So it seems like you are right. Does it matter which web server is used, i.e Apache, IIS, or do they all function the same way when it comes to this? – Ali Jul 06 '12 at 10:28
  • There may be minor differences – Arnaud Le Blanc Jul 06 '12 at 10:29
  • I guess, how fast the abort is detected, etc; You have to test yourself – Arnaud Le Blanc Jul 06 '12 at 10:33
3

See: http://php.net/manual/en/function.ignore-user-abort.php. It depends on the ignore_user_abort setting and a few other conditions.

D-Rock
  • 2,636
  • 1
  • 21
  • 26
1

The default behaviour for PHP is that your script will be aborted when the remote client disconnects, usually caused by a user closing their browser or hitting the stop button. It can also happen when someone closes a remote connection to a script run in CLI mode. The one exception to your script aborting is if you have registered a shutdown function using register_shutdown_function(). For a thorough explanation please see the Connection Handling page in PHP docs.

The point at which the script actually terminates is dependent upon when flush() is called, which essentially attempts to push current output all the way to the browser/client. At this point if the state of the connection is ABORTED then the script will terminate, unless we are ignoring user abort.

The important thing to note is that you can control whether this behaviour occurs or not, as you specify sometimes it is undesirable for this to happen. Using the ignore_user_abort() function or setting the associated directive in php.ini.

GordyD
  • 5,063
  • 25
  • 29
  • `PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client.` http://www.php.net/manual/en/function.ignore-user-abort.php – Ali Jul 06 '12 at 11:19
-2

Unless it's launched as a background process i.e. with the & at the end... it will just terminate.

Closing browser a bit more to it... It will stop eventually, but will process for a little while before web server realises connection is gone.

Brian
  • 8,418
  • 2
  • 25
  • 32