How can I use a php script to determine whether the client browser is disconnected?

Source: Internet
Author: User
How can I use a php script to determine whether the client browser is disconnected? I once saw a person posting: in & nbsp; PHP & nbsp;, the system maintains the connection status. There are three possible states: & nbsp; 0 & nbsp;-& nbsp; NORMAL (NORMAL) 1 & nbsp;-& nbsp; ABORTED (exited) 2 & nbsp;-& nbsp; TIMEOUT (timed out) when & nbsp; PHP & nbsp; how can I use a php script to determine whether the client browser is disconnected?
I once saw a person posting:

In PHP, the system maintains the connection status. There are three possible states:
0-NORMAL (NORMAL)
1-ABORTED (exit)
2-TIMEOUT (TIMEOUT)

When the PHP script runs normally, the connection is valid. When the remote client is disconnected, the ABORTED status mark will be opened. The interruption of 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 mark will be opened.
You can determine whether your script needs to exit when the client is disconnected. Sometimes it will bring a lot of convenience for the complete running of your script, and there is no remote browser to accept the output of the script. By default, the script will exit when the remote client connection is interrupted. This process can be controlled by php. ini's ignore_user_abort or by the "php_value ignore_user_abort" and ignore_user_abort () functions in Apache. conf settings. If you do not tell PHP to ignore user interruptions, your script will be interrupted unless you have disabled the trigger function through register_shutdown_function. Close the departure function. when a remote user clicks the STOP button and your script tries to output data again, PHP will detect that the connection has been interrupted and call the close trigger function.
Your script may also be interrupted by the built-in script timer. The default timeout limit is 30 seconds. This value can be changed by setting the "php_value max_execution_time" parameter in the max_execution_time or Apache. conf setting of php. ini or the set_time_limit () function. When the counter is exceeded, the script will exit when the above connection is interrupted, and the previously registered closed trigger function will also be executed at this time. In this function, you can call the connection_status () function to check whether timeout causes the function to be called. If timeout causes function call to be disabled, the function returns 2.
Note that the ABORTED and TIMEOUT statuses are valid at the same time. This is possible when you tell PHP to ignore user exit operations. PHP will still note that the user has interrupted the connection but the script is still running. If the running time limit is reached, the script will be exited, and the set function to close and trigger will also be executed. At this time, you will find that the connection_status () function returns 3.



However, my test result is that if the backend does not output content to the front end, for example, in an endless loop While processing, the connection_status () will always be 0, the front-end is disconnected.

Share: More


------ Solution --------------------
Yes! Php checks whether the user connection is valid only when executing the output statement.
If you want to detect it in time, you need to modify the php kernel.
But the cost for doing so is too high.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.