PHP File compression zlib. output_compression and ob_gzhandler,

Source: Internet
Author: User

PHP File compression zlib. output_compression and ob_gzhandler,


Source:
\ ThinkPHP3.1.3 _ full \ ThinkPHP \ Lib \ Core \ App. class. php init () method
If (C ('output _ encode ')){
$ Zlib = ini_get ('zlib. output_compression ');
If (empty ($ zlib) ob_start ('ob _ gzhandler ');
}

Zlib. output_compression and ob_gzhandler are the methods for compressing the page intranet,
Ob_gzhandler () and zlib. output_compression cannot be used at the same time.
Note that zlib. output_compression is better than ob_gzhandler ().


There are three ways to use the ob_gzhandler function to compress php:
1. Set output_handler = ob_gzhandler in php. ini.
2. Add php_value output_handler ob_gzhandler to. htaccess.
3. Add ob_start ('ob _ gzhandler') to the PHP file header ');


Zlib. output_compression method:
Open the php. ini file in the php Directory, find zlib. output_compression = Off, and change it to zlib. output_compression = On,
Remove the; zlib. output_compression_level; and change-1 to 1 ~ The value of 5,
In this way, the gzip effect of all php pages can be achieved.
Note the following:
I. zlib. output_handler must be commented out because this parameter conflicts with the previous settings-the official statement.
2. Generally, the cache is 4 k (output_buffering = 4096 ).
3. The recommended parameter value of zlib. output_compression_level is 1 ~ 5 or 6, the actual compression effect is not significant, but the cpu usage is a geometric increase.


Example01: ob_gzhandler Method for earlier IE versions:

<?php/*The Accept-Encoding header can't be trusted in IE5 and unpatched IE6;there are 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:*/ function isBuggyIe() {     $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"); 

 




Example02: css/jss File Processing

<? Php/* It is also possible to use ob_gzhandler to compress css and javascript files, however some browsers such as firefox keep CT 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"); // or header ("Content-Type: text/javascript"); header ("Content-Length :". ob_get_length (); ob_end_flush ();

 



// To be supplemented...




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.