HTTP browser Active disconnect with PHP active disconnect

Source: Internet
Author: User
Tags php server

This article for everyone to share the HTTP browser active disconnection and PHP active disconnection, interested friends can take a look

Abstract: The cause of the event is due to doubts that are encountered during development. Once the browser client was actively disconnected, it was discovered that the server-side PHP script was still executing so that it did not know how to stop the script. There is also a need for PHP script to actively disconnect, and then subsequent scripts continue to execute (a time-consuming task), so with this blog.

One, the browser actively disconnect

In the common lamp combination, we think that the browser accesses a PHP script, the script starts executing, the script outputs the content, and ends the run, Apache responds to HTTP, the browser receives an HTTP response, and the results are displayed.
Down to consider the special situation.
  1, the browser sends an HTTP request, PHP executes a time-consuming task (20s)(assuming PHP set_time_limit setting is 30s), during which the browser is unresponsive, the user clicks on Browser x, the browser actively disconnects, Whether or not the PHP script continues to run.
Assuming that the time-consuming task is: Calculate FIB (25), the browser test response takes time 1.15s, each time-consuming task, write file log write once, perform 10 time-consuming tasks, in the execution of the 5th time, the client actively disconnects, observe the situation.
The code is as follows:

<?phpfor ($i =0; $i < $i + +) {     fib];    Setlog (Date (' h:i:s '));} function fib ($n = 3) {    if ($n = = 0) {        return 1;    }    if ($n = = 1) {        return 1;    }    Return Fib ($n-1) + FIB ($n-2);} function Setlog ($massage, $path = ') {    $log _path = Empty ($path)? '. /log_ '. Date (' y-m-d '). Log ': $path;    $time = Date (' y-m-d h:i:s ');    $error _page = ' http://'. $_server[' Http_host '. $_server[' Request_uri '];    File_put_contents ($log _path, "Log Time:". $time. Php_eol, file_append);    File_put_contents ($log _path, "Log URL:". $error _page. Php_eol, file_append);    if (Is_array ($massage)) {        $massage = Json_encode ($massage);    }    File_put_contents ($log _path, "Log MESSAGE:". $massage. Php_eol. Php_eol, file_append);}? >

The browser disconnects the connection when it executes to 5.44s.
The log shows that the script executed 10 loops.
This is not the same as what we thought before;

  2, optimize, see Online said, PHP to determine whether the client connection is disconnected, is in PHP to the client output content when judged , then we have to modify the test code:

<phpfor ($i =0; $i < $i + +) {     fib];    Setlog (Date (' h:i:s '));    echo "Hello";} The FIB and Setlog functions are omitted here?>

Test it again and find the same results as the last Test. The reason: When PHP output content to the client, there are 3 buffering stages, respectively:
PHP buffer = web server buffer = Browser buffer
It is only when the buffer is full that it is output to the client, which is actually the principle that the backend outputs the content to the front end at intervals. Of course, you can also control when the buffer is not full, and let the output to the client.

  3, then modify the test code, so that the content of the output client is large enough:

<?php$re = ""; for ($i = 0; $i < 10000; $i + +) {    $re. = "AA";} for ($i =0; $i <; $i + +) {     fib];    Setlog (Date (' h:i:s '));    echo $re;} The FIB and Setlog functions are omitted here?>

This time again, you will find that the browser will receive some of the corresponding in a period of time, rather than the previous demo, the script needs to be completely finished before the output to the client. Meanwhile, when the client connection is closed at this time, the server side will check that the client connection has been disconnected when it outputs the content to the client again, and the script will stop running. This is the test result we want.

  4, and then modify the test code, this time do not let the output of a very large content, but deliberately manipulate the buffer content, so that although not enough from the buffer output to the client's content in advance output to the client.
Test code:

for ($i =0; $i <; $i + +) {     fib];    Setlog (Date (' h:i:s '));    echo "Hello". Date (' H:i:s '). "<br>";    Ob_flush ();    Flush ();} The FIB and Setlog functions are omitted here.

Summary:

    • In principle, the client is actively disconnected, the PHP script will stop running;

    • But the premise is that PHP knows how the client disconnects, only when PHP output content to the client (not the PHP buffer, not the Web server buffer), PHP will know that the client connection is interrupted before it stops running;

    • PHP output content to the client, there are two ways. One is to fill the content into the buffer automatically sent to the client, and the second is to use the Ob_flush,flush function to actively flush the buffer content to the client;

    • PHP script runs are also limited by the internal script timer, which can be configured either in PHP.ini or in the host Apache configuration file, or in scripts through the SET_TIME_LIMT function;

    • When the client actively disconnects, and the PHP script does not stop running, but also restricted to the script timer;

    • When the PHP script is set to Ignore_user_abort (true); The script execution is not stopped even if the client connection is disconnected and the PHP output to the client knows that the client connection is disconnected;

    • Inside PHP, the system maintains the connection state, can be checked by the return value of function connection_status, 0:normal; 1:aborted (disconnect); 2:timeout; Change the state of the detection is also required PHP script output to the client will know, otherwise it has been 0;

    • There is also a function to detect whether the client connection is disconnected (connection_aborted), 0 normal, 1 disconnected.

    • A strange problem is that when the client connection has been disconnected, the PHP script output two times, the status bit will become 1;

Two, PHP server-side active disconnection

For PHP to be actively disconnected, use the Content-length and connection two fields in the HTTP response header, respectively, with the following meanings:

    • Content-length, when the client receives the response header Content-length, when the corresponding body receives the specified size, the connection to the server will be disconnected;

    • Connection, when the client receives a response header connection value of close or keep-alive, decides to close the current TCP connection or continue using the current connection for the next request;

    • The test found that PHP can also be actively disconnected when only specifying conetent-length;

    • In fact, it is PHP active disconnection, in fact, PHP notifies the client to actively disconnect the connection;

Example code:

<?phpecho "Hello World"; test (), for ($i =0; $i <; $i + +) {     fib];    Setlog (Date (' h:i:s '));} function test () {    $size = Ob_get_length ();    Header ("Content-length:".) $size);    Header ("Connection:close");    Ob_flush ();    Flush ();} The FIB and Setlog functions are omitted here?>



< finish >

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.