dongyou5271 2011-05-07 19:04
浏览 29

html压缩问题

i am facing a problem with my server load. I checked all my queries and other stuff's but they are okey finaly i found the problem it comes from this function

  function compress($buffer) {
    $buffer = str_replace("\t", '', $buffer);
    return $buffer;
  }

ob_start("ob_gzhandler");
ob_start("compress"); 

when i remove that function the load is come to normal.

any idea's about this issue.

Regards

  • 写回答

1条回答 默认 最新

  • dsxz84851 2013-02-06 10:45
    关注
    ob_start("ob_gzhandler");
    

    This method is dependant on the Apache server and in addition, the mod_gzip module must be installed and loaded. While using the compression remember, the great thing that compression on the server activated only if browsers requests compressed content, in case the browser does not understand compressed content or does not request for it, the server simply servers plain, uncompressed content!

    The method mentioned above is quick and easy, but the downfalls are that it only works on Apache with mod_gzip. Not only that, but according to the Php manual, that is not the preferred method for gzipping.

    The compression level set to 6 by default. A higher compression level means more load on the server's CPU. You might want to experiment with the levels to find a value that will give you a good compression for an acceptable amount of load on the server.

    To change the compression level, simply insert the following line before ob_start("ob_gzhandler"):

    ini_set('zlib.output_compression_level', 4);
    ob_start("ob_gzhandler"):
    

    and another thing if you are using the PHP gzipping then remember to close the compression and if the gzipping is not closed then it sometimes cause the server load. Flush the output to the browser with ob_end_flush

    ob_end_flush();
    

    Recommandations usage of GZip connect:

    Method 1:

    If you are doing compression on all the pages then I would recommend using .htaccess method because it’s very simple to implement and by .htaccess file you can further configure server configurations. Now to enable gzip compression add the following line to your .htaccess file.

    php_value output_handler ob_gzhandler
    

    Method 2:

    The proper way to use the Gzipping through PHP optimized way is:

    // Include this function on your pages
    function print_gzipped_page() {
    
        global $HTTP_ACCEPT_ENCODING;
        if( headers_sent() ){
            $encoding = false;
        }elseif( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ){
            $encoding = 'x-gzip';
        }elseif( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ){
            $encoding = 'gzip';
        }else{
            $encoding = false;
        }
    
        if( $encoding ){
            $contents = ob_get_contents();
            ob_end_clean();
            header('Content-Encoding: '.$encoding);
            print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
            $size = strlen($contents);
            $contents = gzcompress($contents, 9);
            $contents = substr($contents, 0, $size);
            print($contents);
            exit();
        }else{
            ob_end_flush();
            exit();
        }
    }
    
    // At the beginning of each page call these two functions
    ob_start();
    ob_implicit_flush(0);
    
    // Then do everything you want to do on the page
    echo 'Hello World';
    
    // Call this function to output everything as gzipped content.
    print_gzipped_page();
    

    Hope this would resolve your server loading issue.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题