Home  >  Article  >  Backend Development  >  Three reliable ways to enable gzip compression in PHP

Three reliable ways to enable gzip compression in PHP

WBOY
WBOYOriginal
2016-07-25 08:58:561589browse
  1. output_buffering = Off
Copy code

3, php.ini is

  1. ;output_handler = ob_gzhandler
  2. zlib.output_compression = Off
  3. ;z lib.output_compression_level = -1
Copy the code

4, apache is added as follows:

  1. AddOutputFilter DEFLATE html php js css
Copy code

Method 2, use zlib.output_compression in php.ini 1. Close mod_deflate.so in apache 2, php.ini:

  1. output_buffering = On
Copy code

2, php.ini is

  1. ; output_handler =
  2. zlib.output_compression = Off
  3. zlib.output_compression_level = 6 (Level -1 to 9, the default is 6)
Copy the code

Method 3, use PHP’s built-in function ob_gzhandler (must ensure that php_zlib.dll is installed and opened in the extension) 1. Close mod_deflate.so in apache 2. php.ini is

  1. output_handler = ob_gzhandler
  2. zlib.output_compression = On
  3. ;zlib.output_compression_level = -1
Copy code

Instructions: In method three, if output_handler=ob_gzhandler is set in php.ini, then there is no need to add ob_start('ob_gzhandler'); in the program. Otherwise, it will be double compressed, which will actually report an error, and the general program will judge it.

Set php.ini to be off by default and add it in the program, but if it is your own server, set ini to be on by default. It is recommended to use Type 1 or Type 2: (Type 1 is particularly recommended) At this time, ob_start() can customize the function, written like: ob_start("compress_html"); You can compress html format:

  1. function compress_html($buffer){//Remove comments in the file
  2. $pattern=array("/> *([^ ]*) */","'/*[^*]**/'","/[s]+/","/rn/","/n/","/t /");
  3. $replace=array(">\1<","",""," ","","","");
  4. return ltrim(rtrim(preg_replace($pattern,$replace ,$buffer)));
  5. }
Copy the code

After this, not only the html format can be compressed (gzip can also be used).

Complete configuration plan for method 1: 1. Open mod_deflate.so in apache 2. php.ini is

  1. output_buffering = Off
Copy code

3. php.ini is;

  1. output_handler = ob_gzhandler
  2. ;zlib.output _compression = Off
  3. ;zlib .output_compression_level = -1
Copy the code

4, apache adds as follows:

  1. AddOutputFilter DEFLATE html php js css
Copy the code

Use the following program in the php header that needs to be compressed in html format:

  1. ob_start("compress_html");//You need to turn on zlib.output_compression in php.ini
  2. function compress_html($buffer){//Remove comments in the file
  3. $pattern=array("/> ; *([^ ]*) */","'/*[^*]**/'","/[ s]+/","/rn/","/n/","/t/");
  4. $replace=array(">\1<","",""," ","" ,"","");
  5. return ltrim(rtrim(preg_replace($pattern,$replace,$buffer)));
  6. }
Copy the code

This turns on the gzip compression function of php, and Supports automatic compression and formatting of HTML when viewing source files with a browser, which is very useful. Turn on gzip compression, especially for small webmasters with insufficient funds, bandwidth is a waste of money.



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