Table of Contents
php文件压缩zlib.output_compression 和 ob_gzhandler,
Home php教程 php手册 php文件压缩zlib.output_compression 和 ob_gzhandler,

php文件压缩zlib.output_compression 和 ob_gzhandler,

Jun 13, 2016 am 09:25 AM
php zlib compression and document

php文件压缩zlib.output_compression 和 ob_gzhandler,


问题来源:
\ThinkPHP3.1.3_full\ThinkPHP\Lib\Core\App.class.php 中 init()方法
      if(C('OUTPUT_ENCODE')){
            $zlib = ini_get('zlib.output_compression');
            if(empty($zlib)) ob_start('ob_gzhandler');
      }

zlib.output_compression 和 ob_gzhandler 是压缩页面内网的方法,
不能同时使用ob_gzhandler() 和 zlib.output_compression。
也要注意使用 zlib.output_compression 要优于 ob_gzhandler()。


使用ob_gzhandler函数有3种方法让它对php进行压缩:
1、在php.ini中设置output_handler = ob_gzhandler
2、在.htaccess中加入php_value output_handler ob_gzhandler
3、在php文件头加上ob_start('ob_gzhandler');


zlib.output_compression方法:
打开php目录下的php.ini文件,找到zlib.output_compression = Off,改成zlib.output_compression = On,
再把;zlib.output_compression_level前面的;去掉,后面的-1改成1~5的数值,
这样便可以实现所有php页面的gzip效果。
需要说明的是以下几点:
一、;zlib.output_handler必须保持注释掉,因为此参数和前面的设置冲突——官方的说法。
二、一般情况下缓存是4k(output_buffering = 4096)。
三、zlib.output_compression_level 建议参数值是1~5,6以实际压缩效果提升不大,cpu占用却是几何增长。


example01: ob_gzhandler方法IE低版本的处理:

<?<span>php
</span><span>/*</span><span>
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:
</span><span>*/</span>

 <span>function</span><span> isBuggyIe() {
     </span><span>$ua</span> = <span>$_SERVER</span>['HTTP_USER_AGENT'<span>];
     </span><span>//</span><span> quick escape for non-IEs</span>
     <span>if</span> (0 !== <span>strpos</span>(<span>$ua</span>, 'Mozilla/4.0 (compatible; MSIE '<span>)
         </span>|| <span>false</span> !== <span>strpos</span>(<span>$ua</span>, 'Opera'<span>)) {
         </span><span>return</span> <span>false</span><span>;
     }
     </span><span>//</span><span> no regex = faaast</span>
     <span>$version</span> = (<span>float</span>)<span>substr</span>(<span>$ua</span>, 30<span>);
    </span><span>return</span><span> (
         </span><span>$version</span> < 6
         || (<span>$version</span> == 6  && <span>false</span> === <span>strpos</span>(<span>$ua</span>, 'SV1'<span>))
     );
 }
 
</span><span>//</span><span> usage:</span>
 isBuggyIe() || <span>ob_start</span>("ob_gzhandler"<span>);
 </span>
Copy after login




example02: css/jss文件的处理

<?<span>php
</span><span>/*</span><span>
It is also possible to use ob_gzhandler to compress css and javascript files,
however some browsers such as firefox expect content type text/css on css files.
 To get around this send a content type header:
</span><span>*/</span>

 <span>ob_start</span>('ob_gzhandler'<span>);
 </span>?>
 
.... your css content ...
 
<?<span>php
 </span><span>header</span>("Content-Type: text/css");  <span>//</span><span>或header("Content-Type: text/javascript");</span>
 <span>header</span>("Content-Length: ".<span>ob_get_length</span><span>());
 </span><span>ob_end_flush</span><span>();
 </span>
Copy after login

 



//待补充...

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles