How to check if gzip compression is enabled with PHP?

21,239

Solution 1

For PHP, they'll do fine.

However, if your referring to compression of pages back to clients, you'll also need to check it's enabled in apache (assuming your using apache you'll need the mod_gzip.c OR mod_deflate.c modules).

For instance: # httpd -l (apache 2)

Ive also seen mention of needing to implement .htaccess overrides in the past:

#compress all text & html:
AddOutputFilterByType DEFLATE text/html text/plain text/xml
# Or, compress certain file types by extension:
<Files *.html>
SetOutputFilter DEFLATE
</Files>

Solution 2

With

<?php
phpinfo();
?>

You could find if the module is loaded

or

This website https://www.giftofspeed.com/gzip-test/

You can check if the compression on a certain page is enabled.

With those you'll see if the compression is enough for you.

Solution 3

you can do this programmatically from php:

if (count(array_intersect(['mod_deflate', 'mod_gzip'],  apache_get_modules())) > 0) {
    echo 'compression enabled';
}

This is of course not super reliable, because there might be other compression modules...

Share:
21,239
thelolcat
Author by

thelolcat

Updated on February 12, 2022

Comments

  • thelolcat
    thelolcat over 2 years

    Is (function_exists('ob_gzhandler') && ini_get('zlib.output_compression')) enough ?

    I want to check if the host is serving compressed pages within one of the pages :)

  • Warface
    Warface over 5 years
    @Philipp I've changed it for a working one. Search gzip compression checker on google ;)