Php can use set_time_limit (0); to cancel the php step timeout limit, so as to achieve the effect of persistent connections.
The sample code is as follows:
Copy codeThe Code is as follows: <? Php
Echo "output once every 3 seconds <br/> ";
Set_time_limit (0); // ensure that the php program does not exit without timeout
While (1 ){
Echo date ("H: I: s"). "<br/> ";
Ob_flush ();
Flush (); // refresh and output PHP buffer data
Sleep (3); // delay of 3 seconds
}
?>
Sample Code 2:
Copy codeThe 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. '<br> ';
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.
Copy codeThe Code is as follows: for ($ I = 10; $ I> 0; $ I --)
{
Echo $ I. '<br/> ';
Ob_flush ();
Flush ();
Sleep (1 );
}
Ob_end_flush ();