zoukankan      html  css  js  c++  java
  • 【转】apache/php 开启 gzip压缩

    1、php方式开启

    原理:

    header("Content-Encoding: gzip");
    echo gzencode('songjiankang');

     示例1:

    复制代码
    function ob_gzip ($content) // $content 就是要压缩的页面内容,或者说饼干原料
    {
        if (! headers_sent() &&     // 如果页面头部信息还没有输出
        extension_loaded("zlib") &&     // 而且zlib扩展已经加载到PHP中
        strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip"))     // 而且浏览器说它可以接受GZIP的页面
        {
            $content = gzencode($content . " 
    //此页已压缩", 9); // 此页已压缩”的注释标签,然后用zlib提供的gzencode()函数执行级别为9的压缩,这个参数值范围是0-9,0表示无压缩,9表示最大压缩,当然压缩程度越高越费CPU。
                                                                      
            // 然后用header()函数给浏览器发送一些头部信息,告诉浏览器这个页面已经用GZIP压缩过了!
            header("Content-Encoding: gzip");
            header("Vary: Accept-Encoding");
            header("Content-Length: " . strlen($content));
        }
        return $content; // 返回压缩的内容,或者说把压缩好的饼干送回工作台。
    }
    
    ob_start('ob_gzip');
    复制代码

     示例2:

    #ob_gzhandler 为php内置函数,具体参考手册
    ob_start('ob_gzhandler');

    2、apache的方式开启:

    a.开启模块:

    LoadModule deflate_module modules/mod_deflate.so
    LoadModule headers_module modules/mod_headers.so

    b.httpd.conf中增加

    <ifmodule deflate_module>
      DeflateCompressionLevel 9
      AddOutputFilterByType DEFLATE text/html text/plain text/xml application/json application/xml
      AddOutputFilter DEFLATE js css
      AddOutputFilter INCLUDES .shtml .htm .xml .php .html
    </ifmodule>

    c.重启服务器

    原文:http://www.cnblogs.com/siqi/p/4003421.html

  • 相关阅读:
    关于IE缓存的解决方案(HTML,JSP,ASP,PHP,C#)(转)
    ASP.NET 2.0 中的客户端脚本
    Post和Get的区别(兼谈页面间传值的方式)(转)
    委托的小例子
    JS的正则表达式
    HTTP消息头
    asp.net一个onclick的全过程(简单写一下)
    location.reload() 和 location.replace()的区别和应用
    使用 .NET Framework 2.0 在您的应用程序中支持证书(转)
    页面动态注册脚本(小技巧)
  • 原文地址:https://www.cnblogs.com/GaZeon/p/5526591.html
Copyright © 2011-2022 走看看