PHP actively disconnects from the browser. PHP actively disconnects from the browser. it once compiled an article about in-depth analysis of set_time_limit (), connection_status (), and ignore_user_abort () in PHP connection processing. is to explain that the browser PHP actively disconnects from the browser
I have compiled an article about in-depth analysis of set_time_limit (), connection_status (), and ignore_user_abort () in PHP connection processing, which explains how to process PHP scripts on the server when the browser client is disconnected.
This article will explain how the PHP script of the server actively disconnects from the browser. the main method is to use the Content-Length and Connection in the http header.
The function of Content-Length: after the browser receives the message entity of the specified Content-Length, it will disconnect from the server.
Connection: after the browser receives the Close or Keep-Alive Connection, it determines whether to Close the Connection or continue to use the current Connection for the next request.
/**
* Automatically disconnect from the browser
* Jiaofuyou
*/
Echo '000000'; // content output to the browser
{// Code for disconnecting
$ Size = ob_get_length ();
Header ("Content-Length: $ size"); // tells the browser the data Length. after the browser receives the data of this Length, it no longer receives the data.
Header ("Connection: Close"); // tells the browser to Close the current Connection, that is, short Connection.
Ob_flush ();
Flush ();
}
Error_log (date ("[Y-m-d H: I: s]"). "> ". "start ". "\ n", 3, "/usr/local/apache2219/logs/php_log ");
// The duration of the disconnection operation
Sleep (5 );
Echo 'test213'; // cannot be received by the browser
Error_log (date ("[Y-m-d H: I: s]"). "> ". "end ". "\ n", 3, "/usr/local/apache2219/logs/php_log ");
// Check whether the error log is executed after 5 seconds.
?>
Note:
1. Using Content-length alone is actually not disconnected, but the browser stops receiving information, and Connection: Close is actually telling the browser to Close the Connection.
2. specifying Content-Length does not make any sense for file_get_contents. to use it, use curl.
If you want PHP to continuously output content to the browser:
Echo & quot; 1234567890 & quot"
Ob_flush ();
Flush ();
This will not be immediately output to the browser.
Echo & quot; 1234567890
"
// Output to the browser immediately when there is a change
Ob_flush ();
Flush ();
Or:
Echo & quot; 1234567890 & quot"
Print str_pad ("", 10000); // output enough content
Ob_flush ();
Flush ();
The hacker once compiled an article about set_time_limit (), connection_status (), and ignore_user_abort () in PHP connection processing, which explains the browser...