PHP actively disconnects from the browser
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 disconnect, server PHP script processing.
This article will explain how the server PHP script proactively disconnects from the browser, primarily using the HTTP protocol header content-length and connection
Content-length: When a browser receives a message entity of the specified content-length size, the connection to the server is disconnected.
Connection: When a browser receives connection close or keep-alive, it decides whether to turn off the connection or continue to use the current connection to make the next request.
/**
* Automatically disconnect from the browser
* Jiaofuyou
*/
Echo ' 1234567890 '; What is exported to the browser
{//Disconnected code
$size =ob_get_length ();
Header ("Content-length: $size"); Tells the browser the length of the data, the browser will not receive the data after the length of data
Header ("Connection:close"); Tell 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 ");
Perform long operation after disconnecting
Sleep (5);
Echo ' test213 ';//The browser is not receiving
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 for 5 seconds before execution.
?>
Description
1, the use of content-length in fact the connection is not disconnected, only the browser to stop receiving information, Connection:close is really tell the browser to close the connection.
2, the designation Content-length has no meaning for file_get_contents, if want to use, please use curl.
If you want PHP to keep outputting content to the browser:
echo "1234567890"
Ob_flush ();
Flush ();
This is not immediately output to the browser, you can do this
echo "1234567890
"
When a line is changed, it is immediately exported to the browser
Ob_flush ();
Flush ();
Or:
echo "1234567890"
Print Str_pad ("", 10000); More than enough content to output
Ob_flush ();
Flush ();