PHP actively disconnects from the browser _ PHP Tutorial

Source: Internet
Author: User
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...

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.