ThinkPHP 3.1 New Feature Support for Page Compression Output

  • 2021-07-02 23:39:21
  • OfStack

At present, most browsers already support compressed output of pages, and the page size can be reduced by 30% by compressing output. However, since 3.0 and previous versions do not have built-in compressed output function of pages, generally speaking, developers need to add:


ob_start('ob_gzhandler');

However, due to different server environments, this configuration sometimes conflicts with the zlib compression configuration in the php. ini file. However, ThinkPHP3.1 has built-in page compression output function, so it is no longer necessary to manually add ob_gzhandler code, add OUTPUT_ENCODE configuration parameters, and support detecting zlib.output_compression.

By default, the framework will perform page compression output, and automatically detect the configuration of zlib.output_compression. If zlib.output_compression in php. ini is turned on, the page compression will still be performed in the page compression mode of server environment.

There is only one line of relevant code:


if(!ini_get('zlib.output_compression') && C('OUTPUT_ENCODE')) ob_start('ob_gzhandler');

Under some special circumstances, if an error prompt similar to the following appears:


output_handler "ob_gzhandler" conflicts with "zlib.output_compression" 

1 is due to the conflict caused by other compression methods configured on your server. At this time, you can manually turn off OUTPUT_ENCODE, namely:


'OUTPUT_ENCODE'=>false

You can solve the problem.


Related articles: