PHP actively disconnects from the browser

Source: Internet
Author: User

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


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.