Output buffers in PHP

Source: Internet
Author: User
Tags flush functions include integer return sleep client


In PHP, when the echo,print is executed, the output is not immediately transmitted via TCP to the client browser, but the data is written to PHP buffer. The PHP output_buffering mechanism means that a new queue is established before TCP buffer, and the data must pass through the queue. When a PHP buffer is full, the script process passes the output data from the PHP buffer to the system kernel to be displayed by TCP to the browser. So, the data will be written to these places in turn echo/pring-> PHP buffer-> TCP buffer-> browser


There are three functions associated with refreshing buffering in PHP:

1). Flush
Refreshes the buffering of PHP programs, regardless of the circumstances in which PHP executes. This function sends all the output of the program so far to the user's browser. However, the function does not have any effect on the caching mode of the server or client browser, nor does it have any effect on the caching of PHP itself.

2). Ob_flush

This function outputs the cache of PHP itself. The cache of PHP itself is controlled by the output_buffering in php.ini. Ob_flush () is the role of the original output cache is removed, set to wait for the output state, but not directly to the client, then you need to use Ob_flush () and then use Flush (), the client can immediately obtain the output of the script.

The two PHP configurations associated with PHP's own output buffering are:
parameter 1: output_buffering: On/off or integer. When set to ON, output-cache control is used in all scripts, without limiting the size of the cache. When set to an integer, such as output_buffering=4096, the flush cache is automatically output when the cached data reaches 4096 bytes. The difference in this parameter is what causes the above code to perform differently at different times. When output_buffering closes, all output (echo) of the script is sent to the client immediately, and the code is executed with a number per second. When the output_buffering is turned on, the output will slow down the service side until the script is finished before sending it to the client.
parameter 2: Implicit_flush: On/off. Setting on means that when the script has output, it is automatically sent to the client immediately. Equivalent to automatically add flush () after Echo.


3). Ob_implicit_flush

This function forces the output to be sent to the browser whenever there is an output. This does not require each output (echo) to be sent to the browser with flush ().

Example


  1. Ob_end_clean ();
  2. echo str_pad (" ", 256);
  3. For ($i = 100; $i >0; $i-) {
  4. echo $i, '
    ';
  5. Flush ( );
  6. sleep (1);
  7. }
  8. ?> The above code should output $i once every second.The above Echo str_pad ("", 256) is intended for IE to receive 256 bytes before starting the display. The above code also has the following two kinds of wording.
    1. echo str_pad (" ", 256);
    2. For ($i = 100; $i >0; $i-) {
    3. echo $i, '
      /> ';
    4. Ob_flush ( ); Sometimes only flush is impossible.
    5. Flush ( );
    6. sleep (1);
    7. }
    8. ?>
      View Plain
      1. Ob_implicit_flush (TRUE);
      2. echo str_pad (" ", 256);
      3. For ($i = 100; $i >0; $i-) {
      4. echo $i, '
        /> ';
      5. Ob_flush ( );
      6. sleep (1);
      7. }
      8. ?>
        In addition, we need to note that the refresh buffer is not only affected by the above, but also affected by the following:

        1. Individual Web server programs, especially Web server programs under Win32, still cache the output of the script until the end of the program before sending the results to the browser. Some Apache modules, such as Mod_gzip, may have their own output caching, which will result in the results of the flush () function not being immediately sent to the client browser. Even browsers will cache what they receive before it is displayed. For example, Netscape browser caches content before it accepts the start of a newline or HTML tag, and does not display the entire table until the tag is accepted. Some versions of Microsoft Internet Explorer will only start displaying the page after the 256 bytes received, so you must send some extra spaces to allow the browsers to display the page content.


        Here is a very simple piece of code



               -->cache_file= $cache _file;			
        		$this->cache_time= $cache _time;
        			}//Cache start function Cache_start () {if ($this->cache_active) {include ($this->cache_file);
        	Exit
        		
        	//Open cache Ob_start (); }//To determine if the cached file exists and available function cache_active () {//To determine if the file exists if (file_exists ($this->cache_file)) {$last _time=
        					@filemtime ($this->cache_file);//Get last Modified/Determine whether time is available if ($this->cache_time< $last _time) {//available, included in direct display
        					return true;
        						}else{//delete the cache and re-establish the cache unlink ($this->cache_file);
        					return false; Cache_creat () {//////////////////////Cache directory generation function () {///without judgment directly generating cache file directories and files, looping to generate files $file =explode ("/", $this
        			; cache_file);
        			$num =count ($file)-1; For ($i =0 $i < $num; $i + +) {$tm. = $file [$i].
        				/";
        				if (!file_exists ($TM)) {mkdir ($TM);
        		}}///Cached Output function Cache_end () {$cache _content=ob_get_contents ();
        		$this->cache_creat ();	
        		$fp = @fopen ($this->cache_file, "w+"); Fwrite ($fp, $cache _coNtent);
        	Ob_end_flush ();
        		}//Cache scavenging function Cache_clean () {if (unlink ($this->cache_file)) {return true; }else {$this->alert (cache deletion failed!)
        			Please check that the cache file exists ");
        		return false;
        		
        	}//define the reminder function for the cached file functions alert ($a) {echo "<script>alert (' $a ');</script>";
        
         }}?> test page test.php
               -->Cache_start (); At the beginning of the page-------the page output $like = "I love orange bananas!" 
        "; echo $like. "
        "; $my _cache->cache_end ()//FINAL output




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.