【问题标题】:How to check if gzip compression is enabled with PHP?如何检查 PHP 是否启用了 gzip 压缩?
【发布时间】:2012-02-22 15:04:10
【问题描述】:

(function_exists('ob_gzhandler') && ini_get('zlib.output_compression'))够了吗?

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

【问题讨论】:

    标签: php compression gzip


    【解决方案1】:

    对于 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>
    

    【讨论】:

      【解决方案2】:

      <?php
      phpinfo();
      ?>
      

      可以查看模块是否加载

      本站https://www.giftofspeed.com/gzip-test/

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

      你会看到压缩是否足够。

      【讨论】:

      • @Philipp 我已经把它改成了一个工作的。在谷歌上搜索gzip compression checker ;)
      【解决方案3】:

      您可以通过 php 以编程方式执行此操作:

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

      这当然不是超级靠谱,因为可能还有其他的压缩模块……

      【讨论】:

        【解决方案4】:

        我有一个类,如果启用 gzip 压缩,则根据对 css 或 js 文件的 get 请求进行检查,然后尝试输出相应的压缩 css 或 js 文件(如果已启用)或常规文件(如果未启用)。

        如果您只查看如何检查 gszip 是否已启用,那么这些类的 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;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-06
          • 2011-10-11
          • 2011-08-28
          • 2016-05-10
          • 1970-01-01
          • 2013-12-29
          • 2021-02-02
          • 1970-01-01
          相关资源
          最近更新 更多