6

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 :)

4 Answers 4

5

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>
4

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.

1
  • @Philipp I've changed it for a working one. Search gzip compression checker on google ;)
    – Warface
    Nov 26, 2018 at 20:11
2

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...

1
  • Does apache_get_modules() work on NGINX servers?
    – Azamat
    Sep 17, 2022 at 13:18
0

I have a class that checks based on a get request to css or js file if gzip compression is enabled and then tries to output corresponding compressed css or js file if it's enabled or a regular file if it's not.

If you are looking only at how to check if gszip is enabled then isPhpGzCompressionInProcess method of these class can help you but in most cases, you need to process some output base on it and I am pretty sure that the rest of the class can help someone too.

<?php

/**
 * Class AssetBundleController
 */
class AssetBundleCompressionController
{
    /**
     * AssetBundleController constructor.
     */
    public function __construct()
    {
        $this->outputCompression();
    }

    /**
     * Trying to output compression bundle.
     */
    protected function outputCompression()
    {
        // here request to css or js file
        if (empty($_GET['vcv-script']) && empty($_GET['vcv-style'])) {
            return;
        }

        error_reporting(0);
        $mimeType = $this->getMimeType();
        header('Content-Type: ' . $mimeType);

        if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) {
            // browser cannot accept compressed content, so need output standard JS/CSS
            echo file_get_contents($this->getBundlePath());
        } else {
            if ($this->isPhpGzCompressionInProcess()) {
                // let 3 party app gzip our content.
                echo file_get_contents($this->getBundlePath());
            } else {
                // output our gzip content.
                header("Content-Encoding: gzip");
                echo file_get_contents($this->getBundlePath(true));
            }
        }

        exit;
    }

    /**
     * Get current requested bundle path.
     *
     * @param bool $isCompress
     *
     * @return string
     */
    protected function getBundlePath($isCompress = false)
    {
        $assetType = $this->getAssetType();

        $name = $this->getCompressionRequestName($assetType);

        $path = VCV_PLUGIN_DIR_PATH . 'public/dist/' . $name . '.bundle.' . $assetType;

        if ($isCompress) {
            $path .= '.gz';
        }

        return $path;
    }

    /**
     * Check if php compression is already enabled.
     *
     * @return bool
     */
    protected function isPhpGzCompressionInProcess()
    {
        if (in_array('ob_gzhandler', ob_list_handlers())) {
            return true;
        }

        // check if zlib php exention is working
        if (extension_loaded('zlib')) {
            @ini_set('zlib.output_compression_level', 1);

            if (ini_get('zlib.output_compression_level') === '1') {
                return true;
            }
        }

        return false;
    }

    /**
     * Get compression request name.
     *
     * @return string
     */
    protected function getCompressionRequestName($assetType)
    {
        $name = '';

        $compressList = [
            'editor',
            'wp',
            'vendor',
            'runtime',
        ];

        $searchKey = $assetType === 'js' ? $_GET['vcv-script'] : $_GET['vcv-style'];

        $key = array_search($searchKey, $compressList);

        if ($key !== false) {
            $name = $compressList[$key];
        }

        return $name;
    }

    /**
     * Check current requested asset type
     *
     * @return string
     */
    protected function getAssetType()
    {
        $type = 'js';

        if (!empty($_GET['vcv-style'])) {
            $type = 'css';
        }

        return $type;
    }

    /**
     * Set current request asset mine type.
     */
    protected function getMimeType()
    {
        $type = 'application/javascript';

        if (!empty($_GET['vcv-style'])) {
            $type = 'text/css';
        }

        return $type;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.