Implementation of PHP settimelimit0 persistent connection

Source: Internet
Author: User
Tags sleep function

Every PHP script limits the execution time, so we need to use set_time_limit to set the execution time of a script to an infinite length, and then use flush () and ob_flush () to clear the Server Buffer, output the return value of the script at any time.

As shown in the following script:

<?php header("Content-Type: text/plain"); set_time_limit(0); $infoString = "Hello World" . "\n"; while( isset($infoString) ) { echo $infoString; ob_flush(); flush(); sleep(5); } ?> 


After execution, every five seconds, we will get a line of Hello world. If we do not press the stop button, the browser will continue loading one line without stopping.

Through this method, we can complete many functions, such as robot crawlers and instant message boards.

 

 

The following content is reprinted:Http://blog.sina.com.cn/s/blog_7193eeac01010imp.html

  

<? Phpecho str_pad ("", 1024 ); // when this sentence does not exist, the browser does not output any data until the end of the sleep function is set. // The reason is as follows: for ($ I = 10; $ I> 0; $ I --) {echo $ I. '<br/>'; ob_flush (); flush (); sleep (1);} ob_end_flush ();?>

 

 

The use of flush and ob_flush is especially 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 following is a code snippet:
For ($ I = 10; $ I> 0; $ I --)
{
Echo $ I. '<br/> ';
Ob_flush ();
Flush ();
Sleep (1 );
}
Ob_end_flush ();

Refresh the buffer of the PHP program, regardless of the circumstances in which PHP is executed (CGI, web server, etc ). This function sends all the output of the program so far to the user's browser.

The flush () function does not affect the cache mode of the server or client browser. Therefore, you must use both the ob_flush () and flush () functions to refresh the output buffer.

Some Web server programs, especially the Web server programs under Win32, will cache the Script output until the program ends before sending the results to the browser.

Some Apache modules, such as mod_gzip, May output the cache by themselves, which will cause the results produced by the flush () function to be not immediately sent to the client browser.

Even the browser caches the received content before it is displayed. For example, the Netscape Browser caches content before it receives a line break or the beginning of an HTML Tag, and does not display the entire table until it receives the </table> tag.

In some versions of Microsoft Internet Explorer, the page is displayed only after 256 bytes are received. Therefore, some extra spaces must be sent for these browsers to display the page content.

 

 

 

Below is another reprinted article: http://www.nqhua.com/2011/09/23/226.html

 

ob_start,flush,ob_flushfor($i=0;$i<10;$i++) {    echo $i.'<br />';    flush();    sleep(1); }

 

A friend who has learned about the PHP cache output control function must be familiar with the above Code. The result is that one number is output every 1 second, and it takes 10 seconds to complete all the output, however, in actual execution, you may find a strange phenomenon. Some people may sometimes behave as expected, in some cases, 10 numbers are output in 10 seconds at a time. I used to get crazy about this. Some friends commented that this is often because the IE cache must be 256 characters long before it can be output. But I have also considered the IE situation before, it can still be sometimes failed. I read the manual carefully today to understand that these unpredictable phenomena have their reasons.

In the past, PHP. ini had two key parameters that would affect PHP's cache output control:

Parameter 1: output_buffering: On/off or an integer. When it is set to on, the output cache is used in all scripts without limiting the cache size. When it is set to an integer, for example, output_buffering = 4096, when the cache data reaches 4096 bytes, the cache will be automatically refreshed. The difference in this parameter is the reason for the different execution results of the above Code at different times. When output_buffering is disabled, all the output (ECHO) of the script will be sent to the client immediately. When the above code is executed, a number is output every second. After output_buffering is enabled, the output content is cached on the server and sent to the client together until the script ends.

Parameter 2: implicit_flush: On/Off. Setting on means that when the script has output, it is automatically sent to the client immediately. It is equivalent to automatically adding flush () after Echo ().

 

Related functions of PHP cache output control:

bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )

The first parameter is the callback function. Optional. You can filter or perform other processing on the cached output. The most common usage is ob_start ('ob _ gzhandler'), which compresses the cached data and sends it to the client.
The second parameter is the cache block size. Optional. If the cached content reaches or the size of the cache block is operated, the cache automatically outputs the content. The default value is 0, indicating that the cache size is not limited until the cache ends. Another special value is 1, which indicates chunk_size = 4096.
The third parameter: whether to erase the cache. Optional. The default value is true. If it is set to false, the cache will not be cleared until the script is executed.

You can use ob_get_contents () to obtain the cached data on the server in the string format. Using ob_end_flush (), the cached data is output and the cache is disabled.
The use of ob_end_clean () will silently clear the data cached by the server without any data or other behavior.
The cache on the server is stacked. That is to say, you can enable another cache ob_start () in the server before enabling ob_start (). However, you must ensure that the number of operations to disable the cache is the same as that to enable the cache.

Ob_start () can specify a callback function to process cached data. If one ob_start () is nested with another
Ob_start (), we assume that the ob_start () of the outer layer is a, the ob_start () Number of the inner layer is B, and each of them has a callback function named functiona and functionb, respectively, so when the data output in the cache B, it will be processed by the funcitonb callback function, and then handed to the functiona callback function of the outer layer for processing, and then output to the client.

In addition, the manual says that for some Web servers, such as Apache, using the callback function may change the current working directory of the program, the solution is to manually modify the working directory in the callback function and use the chdir function. This is not uncommon. Remember to check the manual when encountering this problem.

 

Flush () and ob_flush ()

The use of these two functions is perhaps the most confusing problem for many people. The interpretation of the two functions in the manual is also not detailed, and the difference between them is not clearly pointed out, it seems that both functions are to refresh the output cache. However, in the code at the beginning of this article, if fush () is replaced with ob_flush (), the program cannot be correctly executed. Obviously, they are different. Otherwise, you can directly describe one of them as the alias of another function in the manual. It is not necessary to describe them separately. So what are their differences?

I have studied the instruction of the manual and read the comments from some people in the manual. I think it should be like this:

When the cache is not enabled, the content output by the script is waiting for output on the server side. Flush () can immediately send the content waiting for output to the client side.
After the cache is enabled, the content output by the script is stored in the output cache, and there is no content waiting for the output. You can directly use flush () without sending any content to the client. The function of ob_flush () is to extract the content that already exists in the output cache and set it to wait for the output status, but it will not be directly sent to the client. In this case, you need to first use ob_flush () then use flush () to obtain the Script output immediately.

That is to say, the script at the beginning of this article can be written according to whether the cache is enabled or not, as follows:

Note: The following code does not take into account the issue that the IE cache must be larger than 256 bytes for output. For example, to test in IE, add the following code: "Echo str_repeat ('', 4096) ".

Statement 1:

output_buffering = off implicit_flush=off  for($i=0;$i<10;$i++) {    echo $i.'<br />';    flush();    sleep(1); }

Statement 2:

output_buffering = on implicit_flush=off  for($i=0;$i<10;$i++) {    echo $i.'<br />';    ob_flush();    flush();    sleep(1); }

Statement 3:

output_buffering = off implicit_flush=off  ob_start(); for($i=0;$i<10;$i++) {    echo $i.'<br />';    ob_flush();    flush();    sleep(1); }

Statement 4:

output_buffering = on implicit_flush=off  ob_end_flush(); for($i=0;$i<10;$i++) {    echo $i.'<br />';    flush();    sleep(1); }

Article 5:

output_buffering = on implicit_flush=off  ob_end_clean(); for($i=0;$i<10;$i++) {    echo $i.'<br />';    flush();    sleep(1); }

Statement 6:

Output_buffering = on; implicit_flush = on ob_end_clean (); // or ob_end_flush (); For ($ I = 0; $ I <10; $ I ++) {echo $ I. '<br/>'; sleep (1 );}

Article 7:

Output_buffering = on; implicit_flush = on ob_end_clean (); // or ob_end_flush (); For ($ I = 0; $ I <10; $ I ++) {echo $ I. '<br/>'; flush (); sleep (1 );}

Statement 8:

output_buffering = off implicit_flush=on  for($i=0;$i<10;$i++) {    echo $i.'<br />';    sleep(1); }

 

 

 

 

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.