gzip 压缩是一种节省带宽并加速 php 应用程序的简单有效的方法。 gzip 压缩背后运行的机制如下所述 -
step1浏览器/客户端向服务器请求文件。
step2 服务器向浏览器发送 .zip 文件 (index.html.zip) 作为响应,而不是普通旧的 index.html,因此下载时间和带宽都会减少。
step3 h2>执行完上述步骤后,浏览器会下载压缩文件并解压,然后显示给用户。这样加载网页的速度非常快。
在 apache 服务器中,我们必须将以下内容添加到 .htaccess 文件中以启用 gzip 压缩。
# compress text, html, javascript, css, xml:addoutputfilterbytype deflate text/plainaddoutputfilterbytype deflate text/htmladdoutputfilterbytype deflate text/xmlinaddoutputfilterbytype deflate text/cssaddoutputfilterbytype deflate application/xmladdoutputfilterbytype deflate application/xhtml+xmladdoutputfilterbytype deflate application/rss+xmladdoutputfilterbytype deflate application/javascriptaddoutputfilterbytype deflate application/x-javascript# or, compress certain file types by extension:<files *.html>setoutputfilter deflate</files>
注意在 php 文件中,我们可以启用 gzip 压缩。
<?php if (substr_count($_server[‘http_accept_encoding’], ‘gzip’)) ob_start(“ob_gzhandler”); else ob_start();?>
以上就是如何在php中启用gzip压缩?的详细内容。