Two Methods for enabling gzip compression in PHP

Source: Internet
Author: User
This article mainly introduces two methods for enabling gzip compression in PHP. This article introduces the php configuration file and enabling method and PHP file enabling method. For more information, see

This article mainly introduces two methods for enabling gzip compression in PHP. This article introduces the php configuration file and enabling method and PHP file enabling method. For more information, see

After gzip compression is enabled, the size of the webpage can be reduced by 20% ~ 90%, which can save a lot of bandwidth, thus reducing the page response time and improving the user experience.

Php configuration change method:

The Code is as follows:


Zlib. output_compression = On
; Enable the gzip Function

Zlib. output_compression_level = 4
; Gzip compression level, 1 ~ 9. 3 ~ 5

; Zlib. output_handler =
; Gzip compression method. Comments are recommended.

PHP file modification method:

We recommend that you write it at the beginning of the page to a public file in the header.

The Code is as follows:


If (! Headers_sent () & // If the page header information has not been output
Extension_loaded ("zlib") & // php has loaded the zlib Extension
Strstr ($ _ SERVER ["HTTP_ACCEPT_ENCODING"], "gzip") // and the browser accepts GZIP
{
Ini_set ('zlib. output_compression ', 'on ');
Ini_set ('zlib. output_compression_level ', '4 ');
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.