Three reliable methods for enabling gzip compression in php

Source: Internet
Author: User
Three reliable methods for enabling gzip compression in php

  1. Output_buffering = Off

3. php. ini is

  1. ; Output_handler = ob_gzhandler
  2. Zlib. output_compression = Off
  3. ; Zlib. output_compression_level =-1

4. Add apache as follows:

  1. AddOutputFilter DEFLATE html php js css

Method 2: Use zlib. output_compression1 in php. ini to disable mod_deflate.so2 and php. ini in apache:

  1. Output_buffering = On

2. php. ini is

  1. ; Output_handler =
  2. Zlib. output_compression = Off
  3. Zlib. output_compression_level = 6 (Level-1 to 9, 6 by default)

Method 3: Use the php built-in function ob_gzhandler (php_zlib.dll must be installed and enabled in extension) 1, disable mod_deflate.so2 in apache, and php. ini is

  1. Output_handler = ob_gzhandler
  2. Zlib. output_compression = On
  3. ; Zlib. output_compression_level =-1

Note: If php is used in method 3. if output_handler = ob_gzhandler is set in ini, you do not need to add ob_start ('OB _ gzhandler ') to the program. Otherwise, the program will compress the data and report an error. the general program will judge it.

Set php. ini to disabled by default, and add it to the program. if it is your own server, set ini to enabled by default. We recommend that you use 1st or 2nd types: (1st types are recommended) in this case, ob_start () can be used to customize functions, such as: ob_start ("compress_html"); to compress the html format:

  1. Function compress_html ($ buffer) {// remove comments from the file
  2. $ Pattern = array ("/> * ([^] *) * /"," '/\ * [^ *] * \ */' ","/[\ S] +/","/\ r \ n /", "/\ n/", "/\ t /");
  3. $ Replace = array (">\\ 1 <","","","","","","");
  4. Return ltrim (rtrim (preg_replace ($ pattern, $ replace, $ buffer )));
  5. }

After that, you can not only compress the html format (but also use gzip ).

Complete configuration scheme of Method 1: 1. open mod_deflate.so2 in apache, and set php. ini

  1. Output_buffering = Off

3. php. ini is;

  1. Output_handler = ob_gzhandler
  2. ; Zlib. output_compression = Off
  3. ; Zlib. output_compression_level =-1

4. Add apache as follows:

  1. AddOutputFilter DEFLATE html php js css

Use the following program to compress the php header in html format:

  1. Ob_start ("compress_html"); // you need to enable zlib. output_compression in php. ini.
  2. Function compress_html ($ buffer) {// remove comments from the file
  3. $ Pattern = array ("/> * ([^] *) * /"," '/\ * [^ *] * \ */' ","/[\ S] +/","/\ r \ n /", "/\ n/", "/\ t /");
  4. $ Replace = array (">\\ 1 <","","","","","","");
  5. Return ltrim (rtrim (preg_replace ($ pattern, $ replace, $ buffer )));
  6. }

In this way, php's gzip compression function is enabled, and html is automatically compressed and formatted when the browser views the source file, which is very useful. Enable gzip compression, especially for low-cost webmasters, bandwidth is white.

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.