>백엔드 개발 >PHP 튜토리얼 >php文件压缩zlib.output_compression 和 ob_gzhandler,_PHP教程

php文件压缩zlib.output_compression 和 ob_gzhandler,_PHP教程

WBOY
WBOY원래의
2016-07-13 10:18:541068검색

php文件压缩zlib.output_compression 和 ob_gzhandler,


问题来源:
\ThinkPHP3.1.3_full\ThinkPHP\Lib\Core\App.class.php 中 init()方法
      if(C('OUTPUT_ENCODE')){
            $zlib = ini_get('zlib.output_compression');
            if(empty($zlib)) ob_start('ob_gzhandler');
      }

zlib.output_compression 和 ob_gzhandler 是压缩页面内网的方法,
不能同时使用ob_gzhandler() 和 zlib.output_compression。
也要注意使用 zlib.output_compression 要优于 ob_gzhandler()。


使用ob_gzhandler函数有3种方法让它对php进行压缩:
1、在php.ini中设置output_handler = ob_gzhandler
2、在.htaccess中加入php_value output_handler ob_gzhandler
3、在php文件头加上ob_start('ob_gzhandler');


zlib.output_compression方法:
打开php目录下的php.ini文件,找到zlib.output_compression = Off,改成zlib.output_compression = On,
再把;zlib.output_compression_level前面的;去掉,后面的-1改成1~5的数值,
这样便可以实现所有php页面的gzip效果。
需要说明的是以下几点:
一、;zlib.output_handler必须保持注释掉,因为此参数和前面的设置冲突——官方的说法。
二、一般情况下缓存是4k(output_buffering = 4096)。
三、zlib.output_compression_level 建议参数值是1~5,6以实际压缩效果提升不大,cpu占用却是几何增长。


example01: ob_gzhandler方法IE低版本的处理:

<?<span>php
</span><span>/*</span><span>
The Accept-Encoding header can't be trusted in IE5 and unpatched IE6;
there are gzip-related bugs in this browsers.
The docs don't mention if ob_gzhandler knows about these,
so you might want to use the function below:
</span><span>*/</span>

 <span>function</span><span> isBuggyIe() {
     </span><span>$ua</span> = <span>$_SERVER</span>['HTTP_USER_AGENT'<span>];
     </span><span>//</span><span> quick escape for non-IEs</span>
     <span>if</span> (0 !== <span>strpos</span>(<span>$ua</span>, 'Mozilla/4.0 (compatible; MSIE '<span>)
         </span>|| <span>false</span> !== <span>strpos</span>(<span>$ua</span>, 'Opera'<span>)) {
         </span><span>return</span> <span>false</span><span>;
     }
     </span><span>//</span><span> no regex = faaast</span>
     <span>$version</span> = (<span>float</span>)<span>substr</span>(<span>$ua</span>, 30<span>);
    </span><span>return</span><span> (
         </span><span>$version</span> < 6
         || (<span>$version</span> == 6  && <span>false</span> === <span>strpos</span>(<span>$ua</span>, 'SV1'<span>))
     );
 }
 
</span><span>//</span><span> usage:</span>
 isBuggyIe() || <span>ob_start</span>("ob_gzhandler"<span>);
 </span>

 




example02: css/jss文件的处理

<?<span>php
</span><span>/*</span><span>
It is also possible to use ob_gzhandler to compress css and javascript files,
however some browsers such as firefox expect content type text/css on css files.
 To get around this send a content type header:
</span><span>*/</span>

 <span>ob_start</span>('ob_gzhandler'<span>);
 </span>?>
 
.... your css content ...
 
<?<span>php
 </span><span>header</span>("Content-Type: text/css");  <span>//</span><span>或header("Content-Type: text/javascript");</span>
 <span>header</span>("Content-Length: ".<span>ob_get_length</span><span>());
 </span><span>ob_end_flush</span><span>();
 </span>

 



//待补充...

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/879487.htmlTechArticlephp文件压缩zlib.output_compression 和 ob_gzhandler, 问题来源: \ThinkPHP3.1.3_full\ThinkPHP\Lib\Core\App.class.php 中 init()方法 if(C('OUTPUT_ENCODE')){ $zlib = ini_...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.