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

后端 未结 4 1561
無奈伤痛
無奈伤痛 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

    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.

提交回复
热议问题