1

I'm using code from this answer to minify HTML and then ob_gzhandler to gzip the page (because mode_deflate is disabled on my shared server and therefore I cannot gzip in .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();

Both ob_start("sanitize_output") and ob_start("ob_gzhandler") work well on their own, but combining them causes a content encoding error:

enter image description here

What can I do?

Community
  • 1
  • 1

1 Answers1

0

You need to put ob_start("ob_gzhandler") before 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();
}

Don't forget to use ob_end_flush(); at the end.

000
  • 3,976
  • 4
  • 25
  • 39