【问题标题】:How to call ob_gzhandler after ob_start has already been called?在 ob_start 已经被调用后如何调用 ob_gzhandler?
【发布时间】:2015-04-12 10:35:44
【问题描述】:

我使用来自this answer 的代码来缩小HTML,然后使用ob_gzhandler 压缩页面(因为mode_deflate 在我的共享服务器上被禁用,因此我无法在.htaccess 中压缩):

function sanitize_output($buffer) {

    $buffer = preg_replace('/[\r\n]+\s*/', '', $buffer);

    return $buffer;
}

ob_start("sanitize_output");
if(!ob_start("ob_gzhandler")) ob_start();

ob_start("sanitize_output")ob_start("ob_gzhandler") 单独运行良好,但将它们组合起来会导致内容编码错误:

我能做什么?

【问题讨论】:

    标签: php output gzip minify


    【解决方案1】:

    你需要把ob_start("ob_gzhandler")放在ob_start("sanitize_output")之前

    function sanitize_output($buffer) {
        $buffer = preg_replace('/[\r\n]+\s*/', '', $buffer);
        return $buffer;
    }
    if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
        ob_end_clean();
        ob_start('ob_gzhandler');
        ob_start("sanitize_output");
    }
    else {
        ob_start();
    }
    

    不要忘记在最后使用ob_end_flush();

    【讨论】:

      猜你喜欢
      • 2011-08-26
      • 1970-01-01
      • 2017-10-13
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      相关资源
      最近更新 更多