php关于ob_start('ob_gzhandler')启用GZIP压缩的bug

如果使用ob_start("ob_gzhandler"); 
则ob_clean()后面的输出将不显示,这是个bug,
可以用ob_end_clean();ob_start("ob_gzhandler"); 代替ob_clean();
否则后面输出内容将是空。

<?php
error_reporting(E_ALL);
ob_start("ob_gzhandler");
echo "content";
ob_clean();
echo "more content";
?>
上面的代码期望输出more content实际上什么内容也不会输出。

下面就正常了
<?php
error_reporting(E_ALL);
ob_start("ob_gzhandler");
echo "content";
ob_end_clean();
ob_start("ob_gzhandler");
echo "more content";
?>

下面自定义一个回调函数再测试
<?php
function my_ob_gzhandler($buffer,$mod){
   header("Content-Encoding: gzip");
   return gzencode($buffer, 9, FORCE_GZIP); 
}

error_reporting(E_ALL);
ob_start("my_ob_gzhandler");
echo "content";
ob_clean();
echo "more content";
?>
上面是正常的,但使用ob_end_clean代替ob_clean后又会导致后面的输出不会显示。

因此即使是下面的代码依然会在使用ob_clean或者ob_end_clean后会导致输出为空。
<?php
if (ini_get('zlib.output_compression')) {
   if (ini_get('zlib.output_compression_level') != 9) {
      ini_set('zlib.output_compression_level', '9');
   } 
   ob_start();
} else {
   if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], "gzip")) {
      ob_start("ob_gzhandler");
   } else {
      ob_start();
   } 

?>

最稳定的启用页面压缩的方法应该类似下面
<?php
if(extension_loaded('zlib')) {
ini_set('zlib.output_compression', 'On');
ini_set('zlib.output_compression_level', '3');
}
?>

但如果一定要使用ob_gzhandler来启用页面压缩就要注意本文的第一句话了。

事实上,下面的代码只是浏览器不显示
error_reporting(E_ALL);
ob_start("ob_gzhandler");
echo "content";
ob_clean();
echo "more content";

但如果测试一下

telnet localhost 80
GET /test.php HTTP/1.0
<Enter>
<Enter>

将会返回如下信息

HTTP/1.1 200 OK
Date: Fri, 20 Feb 2009 15:40:17 GMT
Server: Apache/2.2.6 (Win32) PHP/5.2.5
X-Powered-By: PHP/5.2.5
Vary: Accept-Encoding
Content-Length: 12
Connection: close
Content-Type: text/html

more content

失去了跟主机的连接。

可以看出more content已经输出

但为何浏览器不显示呢?

原文地址:https://www.cnblogs.com/914556495wxkj/p/6206226.html