如何使用PHP检查是否启用了gzip压缩?

6

“(function_exists('ob_gzhandler') && ini_get('zlib.output_compression'))”这个条件足够吗?

我想检查主机是否在其中一些页面中提供了压缩页面 :)

4个回答

5

对于PHP,它们可以很好地完成任务。

然而,如果您需要将页面压缩返回到客户端,则还需要检查是否已在apache中启用(假设您正在使用apache,则需要mod_gzip.c或mod_deflate.c模块)。

例如: # httpd -l(apache 2)

我之前还见过提到需要实现.htaccess覆盖的内容:

#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();
?>

您可以查看模块是否已加载

或者

此网站https://www.giftofspeed.com/gzip-test/

您可以检查某个页面的压缩是否已启用。

通过这些,您可以确认压缩是否足够符合您的需求。


@Philipp 我已经更改为可用的代码。在谷歌上搜索“gzip压缩检查器”即可 ;) - Warface

2
您可以通过PHP编程来实现此操作:
if (count(array_intersect(['mod_deflate', 'mod_gzip'],  apache_get_modules())) > 0) {
    echo 'compression enabled';
}

当然,这并不是非常可靠的,因为可能会有其他压缩模块...

apache_get_modules() 函数在 NGINX 服务器上能否使用? - Azamat

0
我有一个类,根据对css或js文件的get请求检查gzip压缩是否启用,然后尝试输出相应压缩过的css或js文件(如果启用),否则输出常规文件。
如果你只是想知道如何检查gzip是否启用,那么这个类的isPhpGzCompressionInProcess方法可以帮到你,但在大多数情况下,你需要根据其处理一些输出,而我相信该类的其余部分也能帮助别人。
<?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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接