PHP actively disconnects from the browser

Source: Internet
Author: User
Tags flush php script

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 ();

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.