Php compresses the ob_gzhandler function in three ways.

Source: Internet
Author: User

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 ');

The first method is not feasible. The second method is good. Once and for all, the effects of the first and second methods are the same, that is, compression of all php suffix files. Here I will mainly introduce the third method,
The CSS compression method is used as an example to compress style.css under the template.

1. Copy style.cssin the Template directory and name it style.css. php.

Add this sentence to the top of style.css. php:

 

The code is as follows: Copy code
<? Php if (extension_loaded ('zlib ') {ob_start ('OB _ gzhandler');} header ("Content-type: text/css");?>

Add

 

The code is as follows: Copy code
<? Php if (extension_loaded ('zlib ') {ob_end_flush () ;}?>

2. Modify the css connection in header. php as follows

Original:

The code is as follows: Copy code


<Link rel = "stylesheet" type = "text/css" media = "screen" href = "/style.css"/>

Modified:

The code is as follows: Copy code


<Link rel = "stylesheet" type = "text/css" media = "screen" href = "/style.css. php"/>

Now your CSS can be compressed by gzip.

JS can also use the same method, but the header added to xx. js. php should be like this:

 

The code is as follows: Copy code
<? Php if (extension_loaded ('zlib ') {ob_start ('OB _ gzhandler');} header ("Content-Type: text/java script");?>

(Remove when copying? Space before php)

Take a closer look at JS in the K2 topic, that is, add the ob_gzhandler function to the top after modifying the suffix to php.

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.