Home  >  Article  >  Backend Development  >  Two method codes for turning on gzip compression in php

Two method codes for turning on gzip compression in php

怪我咯
怪我咯Original
2017-07-13 14:33:591866browse

Gzip web page compression can greatly improve website access speed. For websites abroad, this is an essential step. It is very obvious to improve the web page opening speed. Now we will systematically understand this Gzip Lushan true colors.

1. What is GZIP

GZIP concept

GZIP was first created by Jean-loup Gailly and MarkAdler and was used for file compression in UNIX systems. We often use files with the suffix .gz in Linux, and they are in GZIP format. Nowadays, it has become a very common data compression format, or file format, used on the Internet.

GZIP encoding over HTTP protocol is a technology used to improve the performance of WEB applications. High-traffic WEB sites often use GZIP compression technology to allow users to experience faster speeds. This generally refers to a function of installation in the WWW server. When someone visits the website in this server, this function in the server will compress the web page content and transmit it to the visiting computer browser for display. Generally, plain text content can be compressed to 40% of the original size. This will make the transmission faster, and the effect is that it will be displayed quickly after you click on the URL. Of course, this will also increase the load on the server. Generally, this is installed on the server. Function module.

2. What are the benefits of turning on GZIP?

After Gzip is turned on, the data output to the user's browser will be compressed, which will reduce the cost of transmission through the network. data volume and increase browsing speed. Gzip can greatly speed up a website. Sometimes the compression ratio is as high as 80%, and at least more than 40%.

This article mainly introduces two methods to enable gzip compression in PHP. This article introduces the php configuration file and the opening method, and the php file opening method. Friends who need it can refer to the following

php configuration modification method:

The code is as follows:

zlib.output_compression = On
;开启gzip功能
zlib.output_compression_level = 4
;gzip压缩级别,1~9,建议3~5
;zlib.output_handler =
;gzip压缩方式,建议注释

php file modification method:

It is recommended to write it at the beginning of the page, you can write to the header public file

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');
}

The above is the detailed content of Two method codes for turning on gzip compression in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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