Home  >  Article  >  Backend Development  >  Output buffering in PHP_PHP tutorial

Output buffering in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:17961browse


In PHP, when echo and print are executed, the output is not immediately transmitted to the client browser for display through tcp, but the data is written to the php buffer. The php output_buffering mechanism means that a new queue is established before the tcp buffer, and data must pass through the queue. When a php buffer is full, the script process will hand over the output data in the php buffer to the system kernel and pass it to the browser via TCP for display. Therefore, the data will be written to these places in sequence echo/pring -> php buffer -> tcp buffer -> browser


There are three functions related to refresh buffering in PHP:

1).flush
Flushes the PHP program's buffer regardless of the circumstances under which PHP is executed. This function sends all the program's output so far to the user's browser. But this function will not have any impact on the caching mode of the server or client browser, nor will it have any impact on the caching of PHP itself.

2).ob_flush

This function outputs the cache of PHP itself. PHP's own caching is controlled by output_buffering in php.ini. The function of ob_flush() is to take out the content originally stored in the output cache and set it to the waiting output state, but it will not be sent directly to the client. In this case, you need to use ob_flush() first and then flush(), the client to get the output of the script immediately.

The two PHP configurations related to output buffering in PHP itself are:
Parameter 1: output_buffering : on/off or integer . When set to on, the output cache control will be used in all scripts without limiting the size of the cache. When set to an integer, such as output_buffering=4096, when the cached data reaches 4096 bytes, the cache will be automatically output and refreshed. The difference in this parameter is the reason why the above code has different execution results at different times. When output_buffering is turned off, all output (echo) of the script will be sent to the client immediately. When the above code is executed, a number will be output every second. After output_buffering is turned on, the output content will be cached first. On the server side, it is not sent to the client until the end of the script.
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.


3).ob_implicit_flush

This function forces output to be sent to the browser immediately whenever there is output. In this way, there is no need to use flush() to send it to the browser after each output (echo).

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 every one second. The above echo The purpose of str_pad(" ", 256) is that IE needs to receive 256 bytes before starting to display. The above code can also be written in the following two ways.
    1. echo str_pad(" ", 256);
    2. for ($i=100; $i>0; $i--) {
    3. echo $i, '
      />';
    4. ob_flush(); //Sometimes flush alone is not enough
    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 also need to pay attention to the fact that the refresh buffer is not only affected by the above aspects, but also affected by the following:

        1). Some web server programs, especially web server programs under Win32, will 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 perform output caching themselves, which will cause the results of the flush() function to not be sent to the client browser immediately. Even browsers cache received content before displaying it. For example, the Netscape browser will Cache content before the beginning of the tag, and after receiving Before marking, the entire table will not be displayed. Some versions of Microsoft Internet Explorer will only begin to display the page after receiving 256 bytes, so some extra spaces must be sent for these browsers to display the page content.


        The following is a very simple piece of code



        <?php
        	/*--------------------编写自己的缓存类---------------*/
        	class my_cache{
        		
        	//定义有关变量
        		private $cache_time;//缓存有效时间
        		private  $cache_file;//缓存文件保存路径
        		
        	//初始化类,默认是index.html时间是1
        		function __construct($cache_file=&#39;index.html&#39;,$cache_time="1"){
        			$this->cache_file=$cache_file;
        			$this->cache_time=$cache_time;			
        		}
        	//缓存开始
        	function cache_start(){
        		
        		if ($this->cache_active){
        			include($this->cache_file);
        			exit;
        	}
        		//开启缓存
        		ob_start();
        		
        	}
        		
        	//判断缓存文件是否存在并且可用
        		function cache_active(){
        				//判断文件是否存在
        			if(file_exists($this->cache_file)){
        				$last_time=@filemtime($this->cache_file);//获取最后修改时间
        				//判断时间是否可用
        				if($this->cache_time<$last_time){
        				//可用,包含进来直接显示
        					return true;
        					}else{
        				//删除该缓存,重新建立缓存
        						unlink($this->cache_file);
        						return false;
        					}				
        			}			
        		}
        		
        	//进行缓存目录的生成
        			function cache_creat(){
        			//不用判断直接生成缓存文件目录及文件,循环生成文件
        			$file=explode("/", $this->cache_file);
        			$num=count($file)-1;
        			for ($i=0;$i<$num;$i++){
        			$tm.=$file[$i]."/";
        				if (!file_exists($tm)){
        					mkdir($tm);
        				}
        			}
        		}
        
        		
        	//缓存的输出
        	function cache_end(){
        		$cache_content=ob_get_contents();
        		$this->cache_creat();
        		$fp=@fopen($this->cache_file, "w+");	
        		fwrite($fp, $cache_content);
        		ob_end_flush();
        	}	
        	
        	//缓存的清除
        	function cache_clean(){
        		if(unlink($this->cache_file)){
        			return true;
        		}else {		
        			$this->alert("缓存删除失败!请检查缓存文件是否存在");
        			return false;
        		}
        		
        	}
        	
        	//定义缓存文件的提醒函数
        	function alert($a){
        		echo "<script>alert(&#39;$a&#39;);</script>";
        		
        	}
        }
        ?>
        
        
        测试页面test.php
        
        <?
        include &#39;cache_my_class.php&#39;;
        $my_cache=new my_cache("./chunge/ge/hao/index.html",5);
        $my_cache->cache_start(); //在页面的最开始 
        
        -------页面输出
         
        $like="我爱吃橘子香蕉!"; 
        echo $like."<br>";
        $my_cache->cache_end();//最后进行输出
        


        www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/777562.htmlTechArticleIn PHP, when echo and print are executed, the output is not immediately transmitted to the client browser through tcp display, instead the data is written to the php buffer. php output_buffering mechanism, meaning in tcp...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn