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

后端 未结 4 1563
無奈伤痛
無奈伤痛 2020-12-05 18:50

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

4条回答
  •  醉酒成梦
    2020-12-05 19:10

    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.

提交回复
热议问题