zoukankan      html  css  js  c++  java
  • ThinkPHP3.1.3源码分析---php文件压缩zlib.output_compression 和 ob_gzhandler


    问题来源:
    ThinkPHP3.1.3_fullThinkPHPLibCoreApp.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低版本的处理:

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




    example02: css/jss文件的处理

    <?php
    /*
    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:
    */
    
     ob_start('ob_gzhandler');
     ?>
     
    .... your css content ...
     
    <?php
     header("Content-Type: text/css");  //或header("Content-Type: text/javascript");
     header("Content-Length: ".ob_get_length());
     ob_end_flush();
     



    //待补充...

  • 相关阅读:
    sencha touch 入门学习资料大全
    细说websocket
    【读fastclick源码有感】彻底解决tap“点透”,提升移动端点击响应速度
    新鲜的前端效果,边栏菜单、滑动效果
    PhoneGap+JQuery Mobile移动应用开发学习笔记
    21个值得收藏的Javascript技巧
    NodeJS无所不能:细数10个令人惊讶的NodeJS开源项目
    Node.js 中文学习资料和教程导航
    PayPal为什么从Java迁移到Node.js 性能提高一倍 文件代码减少44%
    知道创宇研发技能表v2.1
  • 原文地址:https://www.cnblogs.com/dytl/p/3975604.html
Copyright © 2011-2022 走看看