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