0

I use "ob_gzhandler" function for compressing contents. this is my codes:

<?php
ob_start("ob_gzhandler");
?>
<html>
<body>
<p>This should be a compressed page.</p>
</body>
</html> 

don't throw any errors, but compressing operation not working! in "inspect element" isn't "content-encoding : gzip" phrase!

enter image description here

my system information :

Apache/2.4.23 (Win64) PHP/5.6.25 - Port defined for Apache: 80

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Farshad
  • 3
  • 1
  • 5

3 Answers3

5

Php documentation says, that ob_gzhandler requires zlibextension to be installed, so make sure it is true:

 if (extension_loaded('zlib')){
    echo "zlib installed";
}

If it's installed, you can use ob_gzhandler and content will be compressed. The problem is you don't see HTTP header Content-Encoding: gzip in response headers. This header is not set by your web server, but you can set it from php, by means of header() function:

header('Content-Encoding: gzip');

So, combining these two step you have a solution:

if (extension_loaded('zlib') && !ini_get('zlib.output_compression')){
    header('Content-Encoding: gzip');
    ob_start('ob_gzhandler');
}

I also check .iniconfiguration. If zlib.output_compression is On, all output will be compressed, so ob_gzhandler is redundant.

This code is valid, but it's not what php sould be used for. Compressing output and setting headers is web server responsibility. Please, read relevant answer, about configuring .htaccess file, so Apache will do a compression and set correct response headers for you.

Community
  • 1
  • 1
wormi4ok
  • 602
  • 7
  • 11
2

Gotcha 1

ob_start(ob_gzhandler) needs zero preoutput.

(Assert <? is the very first byte of your file.)


Gotcha 2

ob_start(ob_gzhandler) needs zlib.output_compression_level be between -1 and 9.

Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • 1
    I've followed all of the above, but the problem isn't solve ! please help me. tnx – Farshad Apr 07 '18 at 15:42
  • This answer makes literally zero sense. `ob_start(ob_gzhandler)` is the first thing called by PHP and thus would be the first thing output so how could someone possible "fix" this? It's also completely unclear what you are asserting is "wrong" with the OP's code. – cazort Jun 25 '21 at 14:34
0

First ensure that PHP is correctly configured (no restart of any kind required); verified with PHP 7.3.10:

php.ini

zlib.output_compression = On

zlib.output_compression_level = 9

Secondly there are some weird bits. Don't try to manually set the Content-Encoding header, for some reason that seems to break pages. This code must start before any output (including spaces/tabs) is sent to the client:

if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && stristr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip'))
{
 ob_start('ob_gzhandler');
 //header('Content-Encoding: gzip');
}

Lastly you must ensure to properly handle "closing" the compression - if it is active:

if (ob_get_level() > 0) {ob_end_flush();}

I tried all of the above and had to change the last ending bit, hopefully this will save someone's sanity.

John
  • 1
  • 13
  • 98
  • 177