PHP file compression zlib.output_compression and Ob_gzhandler

Source: Internet
Author: User


Source of the problem:
Init () method in \thinkphp3.1.3_full\thinkphp\lib\core\app.class.php
if (C (' Output_encode ')) {
$zlib = Ini_get (' zlib.output_compression ');
if (empty ($zlib)) Ob_start (' Ob_gzhandler ');
}

Zlib.output_compression and Ob_gzhandler are ways to compress the intranet of a page,
You cannot use Ob_gzhandler () and zlib.output_compression at the same time.
Also be aware that using zlib.output_compression is better than Ob_gzhandler ().


There are 3 ways to compress PHP using the Ob_gzhandler function:
1. Set Output_handler = Ob_gzhandler in php.ini
2. Add Php_value output_handler Ob_gzhandler in. htaccess
3, add Ob_start (' Ob_gzhandler ') in PHP file header;


Zlib.output_compression Method:
Open the php.ini file in the PHP directory, find zlib.output_compression = Off, change to zlib.output_compression = ON,
To Zlib.output_compression_level in front of, to remove, and to change the value of 1 in the back.
This will enable the Gzip effect on all PHP pages.
The following points need to be explained:
The Zlib.output_handler must remain commented out, as this parameter conflicts with the previous setting-the official argument.
Second, in general, the cache is 4k (output_buffering = 4096).
Third, Zlib.output_compression_level recommended parameter value is 1~5,6 to the actual compression effect is not increased, CPU consumption is geometric growth.


Example01:ob_gzhandler method The processing of the lower version of IE:

<?PHP/*The accept-encoding header can ' t be trusted in IE5 and unpatched Ie6;there is gzip-related bugs in this browsers. The Docs don ' t mention if Ob_gzhandler knows about These,so you might want to use the function below:*/ functionIsbuggyie () {$ua=$_server[' Http_user_agent ']; //Quick escape for non-ies     if(0!==Strpos($ua, ' mozilla/4.0 (compatible; MSIE ')         ||false!==Strpos($ua, ' Opera ')) {         return false; }     //no regex = Faaast     $version= (float)substr($ua, 30); return (         $version< 6 | | ($version= = 6 &&false===Strpos($ua, ' SV1 '))     ); } //Usage:Isbuggyie () | |Ob_start("Ob_gzhandler"); 




Processing of EXAMPLE02:CSS/JSS files

 <? php  /*  it is also possible to use Ob_gzhandler to compress CSS and JavaScript files,however some BR Owsers such as Firefox expect content type text/css on CSS files. To get around this send a content type header:  */ ob_start  (' Ob_gzhandler ' ? > .... Your CSS content ... <? php  header   ("Content-type:text/css"); //  header  ("Content-length:".) ob_get_length   ());  ob_end_flush   ();  



I want to add ...

PHP file compression zlib.output_compression and Ob_gzhandler

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.