A-A+

php缓冲 output_buffering的使用详解

2018年09月18日 PHP技术文章 评论 1 条 阅读 0 views 次

本篇文章是对php缓冲output_buffering进行了详细的分析介绍,需要的朋友参考下。

buffer

buffer是一个内存地址空间,Linux系统默认大小一般为4096(4kb),即一个内存页。主要用于存储速度不同步的设备或者优先级不同的设备之间传办理数据的区域。通过buffer,可以使进程这间的相互等待变少。这里说一个通俗一点的例子,你打开文本编辑器编辑一个文件的时候,你每输入一个字符,操作系统并不会立即把这个字符直接写入到磁盘,而是先写入到buffer,当写满了一个buffer的时候,才会把buffer中的数据写入磁盘,当然当调用内核函数flush()的时候,强制要求把buffer中的脏数据写回磁盘。

同样的道理,当执行echo,print的时候,输出并没有立即通过tcp传给客户端浏览器显示, 而是将数据写入php buffer。php output_buffering机制,意味在tcp buffer之前,建立了一新的队列,数据必须经过该队列。当一个php buffer写满的时候,脚本进程会将php buffer中的输出数据交给系统内核交由tcp传给浏览器显示。所以,数据会依次写到这几个地方:echo/print -> php buffer -> tcp buffer -> browser

php output_buffering

默认情况下,php buffer是开启的,而且该buffer默认值是4096,即4kb。你可以通过在php.ini配置文件中找到output_buffering配置.当echo,print等输出用户数据的时候,输出数据都会写入到php output_buffering中,直到output_buffering写满,会将这些数据通过tcp传送给浏览器显示。你也可以通过ob_start()手动激活php output_buffering机制,使得即便输出超过了4kb数据,也不真的把数据交给tcp传给浏览器,因为ob_start()将php buffer空间设置到了足够大。只有直到脚本结束,或者调用ob_end_flush函数,才会把数据发送给客户端浏览器。

1.当output_buffering=4096,并且输出较少数据(少于一个buffer),代码如下:

  1. <?php  
  2. for ($i = 0; $i < 10; $i++) {  
  3. echo $i . '<br/>';  
  4. sleep($i + 1); //  
  5. }  
  6. ?>  

现象:不是每隔几秒就会有间断性输出,而是直到响应结束,才能看一次性看到输出,在等待服务器脚本处理结束之前,浏览器界面一直保持空白。这是因为,数据量太小,php output_buffering没有写满。写数据的顺序,依次是echo->php buffer->tcp buffer->browser

2.当output_buffering=0,并且输出较少数据(少于一个buffer),代码如下:

  1. <?php  
  2. //通过ini_set('output_buffering', 0)并不生效  
  3. //应该编辑/etc/php.ini,设置output_buffering=0禁用output buffering机制  
  4. //ini_set('output_buffering', 0); //彻底禁用output buffering功能  
  5. for ($i = 0; $i < 10; $i++) {  
  6. echo $i . '<br/>';  
  7. flush(); //通知操作系统底层,尽快把数据给客户端浏览器  
  8. sleep($i + 1); //  
  9. }  
  10. ?>  

现象:与刚才显示并不一致,禁用了php buffering机制之后,在浏览器可以断断续续看到间断性输出,不必等到脚本执行完毕才看到输出。这是因为,数据没有在php output_buffering中停留。写数据的顺序依次是echo->tcp buffer->browser

3.当output_buffering=4096.,输出数据大于一个buffer,不调用ob_start(),代码如下:

  1. #//创建一个4kb大小的文件  
  2. $dd if=/dev/zero of=f4096 bs=4096 count=1  
  3. <?php  
  4. for ($i = 0; $i < 10; $i++) {  
  5. echo file_get_contents('./f4096') . $i . '<br/>';  
  6. sleep($i +1);  
  7. }  
  8. ?>  

现象:响应还没结束(http连接没有关闭),断断续续可以看到间断性输出,浏览器界面不会一直保持空白。尽管启用了php output_buffering机制,但依然会间断性输出,而不是一次性输出,是因为output_buffering空间不够用。每写满一个php buffering,数据就会发送到客户端浏览器。

4.当output_buffering=4096, 输出数据大于一个tcp buffer, 调用ob_start(),代码如下:

  1. <?php  
  2. ob_start(); //开启php buffer  
  3. for ($i = 0; $i < 10; $i++) {  
  4. echo file_get_contents('./f4096') . $i . '<br/>';  
  5. sleep($i + 1);  
  6. }  
  7. ob_end_flush();  
  8. ?>  

现象:直到服务端脚本处理完成,响应结束,才看到完整输,输出间隔时间很短,以至你感受不到停顿。在输出之前,浏览器一直保持着空白界面,等待服务端数据。这是因为,php一旦调用了ob_start()函数,它会将php buffer扩展到足够大,直到ob_end_flush函数调用或者脚本运行结速才发送php buffer中的数据到客户端浏览器。

output buffering函数

1.ob_get_level

返回输出缓冲机制的嵌套级别,可以防止模板重复嵌套自己。

1.ob_start

激活output_buffering机制。一旦激活,脚本输出不再直接出给浏览器,而是先暂时写入php buffer内存区域。

php默认开启output_buffering机制,只不过,通过调用ob_start()函数据output_buffering值扩展到足够大。也可以指定$chunk_size来指定output_buffering的值。

$chunk_size默认值是0,表示直到脚本运行结束,php buffer中的数据才会发送到浏览器。如果你设置了$chunk_size的大小,则表示只要buffer中数据长度达到了该值,就会将buffer中的数据发送给浏览器。

当然,你可以通过指定$ouput_callback,来处理buffer中的数据。比如函数ob_gzhandler,将buffer中的数据压缩后再传送给浏览器。

2.ob_get_contents

获取一份php buffer中的数据拷贝。值得注意的是,你应该在ob_end_clean()函数调用之前调用该函数,否则ob_get_contents()返回一个空字符中。

3.ob_end_flush与ob_end_clean

这二个函数有点相似,都会关闭ouptu_buffering机制。但不同的是,ob_end_flush只是把php buffer中的数据冲(flush/send)到客户端浏览器,而ob_clean_clean将php bufeer中的数据清空(erase),但不发送给客户端浏览器。ob_end_flush调用之后,php buffer中的数据依然存在,ob_get_contents()依然可以获取php buffer中的数据拷贝。而ob_end_clean()调用之后ob_get_contents()取到的是空字符串,同时浏览器也接收不到输出,即没有任何输出。

惯用案例:

常常在一些模板引擎和页面文件缓存中看到ob_start()使用。下面湿CI中加载模板的程序代码,代码如下:

  1. <SPAN style="WHITE-SPACE: pre">  </SPAN>/* 
  2.    * Buffer the output 
  3.    * 
  4.    * We buffer the output for two reasons: 
  5.    * 1. Speed. You get a significant speed boost. 
  6.    * 2. So that the final rendered template can be 
  7.    * post-processed by the output class.  Why do we 
  8.    * need post processing?  For one thing, in order to 
  9.    * show the elapsed page load time.  Unless we 
  10.    * can intercept the content right before it's sent to 
  11.    * the browser and then stop the timer it won't be accurate. 
  12.    */  
  13.   ob_start();  
  14.   // If the PHP installation does not support short tags we'll  
  15.   // do a little string replacement, changing the short tags  
  16.   // to standard PHP echo statements.  
  17.   if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)  
  18.   {  
  19.                         //替换短标记<?=***>  
  20.    echo eval('?>'.preg_replace("/;*\s*\?>/""; ?>"str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));  
  21.   }  
  22.   else  
  23.   {  
  24.    include($_ci_path); // include() vs include_once() allows for multiple views with the same name  
  25.   }  
  26.   
  27.                 //记录调试信息  
  28.   log_message('debug', 'File loaded: '.$_ci_path);  
  29.   // Return the file data if requested  
  30.   if ($_ci_return === TRUE)  
  31.   {  
  32.    $buffer = ob_get_contents();  
  33.    @ob_end_clean();  
  34.    return $buffer;  
  35.   }  
  36.   /* 
  37.    * Flush the buffer... or buff the flusher? 
  38.    * 
  39.    * In order to permit views to be nested within 
  40.    * other views, we need to flush the content back out whenever 
  41.    * we are beyond the first level of output buffering so that 
  42.    * it can be seen and included properly by the first included 
  43.    * template and any subsequent ones. Oy! 
  44.    * 
  45.    */  
  46.   if (ob_get_level() > $this->_ci_ob_level + 1)  
  47.   {  
  48.    ob_end_flush();  
  49.   }  
  50.   else  
  51.   {  
  52.                         //将模板内容添加到输出流中  
  53.    $_ci_CI->output->append_output(ob_get_contents());  
  54.                         //清除buffer  
  55.    @ob_end_clean();  
  56.   }  
标签:

1 条留言  访客:1 条  博主:0 条

  1. 黑鸟博客

    很有用,学习了..

给我留言