Explore whether the php script will continue to run after the browser is closed

  • 2020-05-17 06:10:50
  • OfStack

Will the php script continue to run after the browser is closed

This problem requires an understanding of PHP's "connection handling" mechanism
Inside PHP, the system maintains the connection state, and there are three possible states:
* 0-NORMAL (normal)
* 1 - ABORTED (exception exit)
* 2-TIMEOUT (timeout)

The connection is valid when the PHP script runs the NORMAL state normally.
When the remote client breaks the connection, the ABORTED status flag is opened. The interruption of a remote client connection is usually caused by the user clicking the STOP button.
When the connection time exceeds the PHP time limit (see the set_time_limit() function), the TIMEOUT status flag is opened.

You can decide whether the script needs to exit when the client breaks the connection. Sometimes it is convenient to have the script run in its entirety, even if no remote browser accepts the script's output. By default, the script exits when the remote client connection is broken. This process can be controlled by the ignore_user_abort of php.ini or by the "php_value ignore_user_abort" and the ignore_user_abort() function corresponding to the Apache.conf Settings. If PHP is not told to ignore user interrupts, the script will be interrupted unless the shutdown trigger function is set via register_shutdown_function(). With this close trigger function, when the remote user clicks the STOP button and the script tries to output data again, PHP will detect that the connection has been broken and call the close trigger function.

Scripts can also be interrupted by built-in script timers. The default timeout limit is 30 seconds. This value can be changed by setting php.ini's max_execution_time or Apache.conf's php_value max_execution_time parameter or the set_time_limit() function. When the counter is timed out, the script exits similar to the connection interruption above, and the previously registered shutdown trigger function is executed. In this close trigger function, you can check whether the timeout caused the close trigger function to be called by calling the connection_status() function. If the timeout results in a call to close the trigger function, the function returns 2.

One thing to note is that the ABORTED and TIMEOUT states can be valid at the same time. This is possible when telling PHP to ignore the user's exit operation. The PHP will still notice when the user has broken the connection but the script is still running. If the run time limit is reached, the script will exit and the set shutdown trigger function will be executed. At this point you will find that the function connection_status() returns 3.


Related articles: