Php-based persistent connection methods and precautions

Source: Internet
Author: User
Tags echo date
This article provides a detailed analysis of the methods and precautions for implementing persistent connections in php. For more information, see set_time_limit (0.

The sample code is as follows:

The code is as follows:


Echo "output once every 3 seconds
";

Set_time_limit (0); // ensure that the php program does not exit without timeout

While (1 ){

Echo date ("H: I: s ")."
";

Ob_flush ();

Flush (); // refresh and output PHP buffer data

Sleep (3); // delay of 3 seconds

}

?>

Sample Code 2:

The code is as follows:


Set_time_limit (0 );

Header ("Connection: Keep-Alive ");

Header ("Proxy-Connection: Keep-Alive ");

For ($ I = 0; $ I <60; $ I ++ ){

Print 'text'. $ I .'
';

Ob_flush ();

Flush ();

Sleep (1 );

Clearstatcache ();

}


Here, ob_flush (); flush (); is called to force data to be output to the buffer zone, so that data can be returned to the browser in a timely manner before the response is returned. In addition, the use of flush and ob_flush is prone to errors, resulting in the failure to refresh the output buffer.

1. the correct order of flush and ob_flush should be: first ob_flush and then flush, as shown below:

Ob_flush ();

Flush ();

If the operating system of the Web server is windows, the order is reversed or the ob_flush () is not used. However, in Linux, the output buffer cannot be refreshed.

2. before using ob_flush (), make sure that the preceding content is 4069 characters long enough.

The output_buffering of some Web servers is 4069 characters or larger by default, that is, the output content must reach 4069 characters before the server will flush refresh the output buffer. to ensure that flush is effective, it is best to set the value in ob_flush () the following statements are available before the function:

Print str_repeat ("", 4096 );

To ensure that the value of output_buffering is reached.

The code is as follows:


For ($ I = 10; $ I> 0; $ I --)

{

Echo $ I .'
';

Ob_flush ();

Flush ();

Sleep (1 );

}
Ob_end_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.