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.