Home > php教程 > php手册 > body text

PHP中开启gzip压缩的2种方法,phpgzip压缩2种

WBOY
Release: 2016-06-13 09:16:01
Original
964 people have browsed it

PHP中开启gzip压缩的2种方法,phpgzip压缩2种

网页开启gzip压缩以后,其体积可以减小20%~90%,可以节省下大量的带宽,从而减少页面响应时间,提高用户体验。

php配置改法:

复制代码 代码如下:


zlib.output_compression = On
;开启gzip功能

zlib.output_compression_level = 4
;gzip压缩级别,1~9,建议3~5

;zlib.output_handler =
;gzip压缩方式,建议注释

php文件改法:

建议在页面开头写上,可以写到头部公共文件

复制代码 代码如下:


if( !headers_sent() && // 如果页面头部信息还没有输出
extension_loaded("zlib") && // 而且php已经加载了zlib扩展
strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip")) //而且浏览器接受GZIP
{
  ini_set('zlib.output_compression', 'On');
  ini_set('zlib.output_compression_level', '4');
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!