Once organized a "about PHP connection processing set_time_limit (), Connection_status () and Ignore_user_abort () in-depth analysis" is to explain the browser client disconnects, the server PHP script processing.
This article will explain how the server PHP script actively disconnects from the browser, the main method is to use the HTTP protocol header content-length and connection
Content-length: When a browser receives a message entity with a specified content-length size, the connection to the server is broken.
Connection: After the browser receives the close or keep-alive of connection, it decides whether to close the connection or continue using the current connection to make the next request.
<?php /** * Automatically disconnects from the browser * jiaofuyou * */ echo ' 1234567890 '; The contents of the output to the browser {//Disconnected code $size =ob_get_length (); Header ("Content-length: $size"); Tells the browser data length, the browser receives this length data and then no longer receives the data header ("Connection:close"); Tells the browser to close the current connection, which is a short connection ob_flush (); Flush (); } Error_log (Date ("[Y-m-d h:i:s]"). ">". " Start "." \ n ", 3,"/usr/local/apache2219/logs/php_log "); Long-time operation after disconnection sleep (5); Echo ' test213 ';//The browser cannot receive Error_log (date ("[Y-m-d h:i:s]"). ">". " End " ." \ n ", 3,"/usr/local/apache2219/logs/php_log "); You can see if the error log is delayed 5 seconds after execution. ?>
Description
1, separate use content-length actually connection is not disconnected, only the browser stop receiving information, Connection:close is really tell the browser to close the connection.
2, the designation Content-length for file_get_contents does not have any meaning; If you want to use it, use curl.
If you want PHP to continue to output content to the browser: echo "1234567890" Ob_flush (); flush (); This is not immediately output to the browser, so echo "1234567890 "// Output to Browser ob_flush (), flush (), OR: echo "1234567890" Print Str_pad ("", 10000) ; Output a sufficient amount of content ob_flush (); flush ();