If your website is in PHP, you can use the following method to accelerate the access speed of your website, no matter how much, even if it is 0.01 seconds, your website has taken another step in user experience.
Before trying to use the following method, please set your firrfox (no Firefox? Install the yslow plug-in developed by Yahoo (what is yslow ?), Use it to analyze the current score of your website. Start gzip compression (This article uses the compressed CSS file as an example ).
Step 1: Create the PHP file required for compression
Copy as followsCode, SaveThe CSS file to be compressed is in the same directory.The name is CSS. php. The name can be named according to your preferences, as long as it is a PHP file.
<? PHP
/**
* @ A. header (... this line sets the compression file type. If you want to compress the JS file, change text/CSS to text/JavaScript.
* @ B. Include (... here contains the files you want to compress. Multiple files are called in sequence by using include ("...");
*/
If ( Extension_loaded ( ' Zlib ' )){ // Check whether zlib expansion is enabled on the server
Ob_start ( ' Ob_gzhandler ' ) ;
}
Header ( " Content-Type: text/CSS; charset: UTF-8 " ) ; // Set the file type to be compressed here
Header ( " Cache-control: Must-revalidate " ) ;
$ Offset = 60 * 60 * 240 ; // The expiration time of the file, which is set to one day
$ Expire = " Expires: " . Gmdate ( " D, D m y h: I: S " , Time () + $ Offset ) . " GMT " ;
Header ( $ Expire ) ;
Ob_start ( " Compress " ) ;
Function Compress ( $ Buffer ) { // Remove comments from the file
$ Buffer = Preg_replace ( ' ! /\ * [^ *] * \ * + ([^/] [^ *] * \ * + )*/! ' , '' , $ Buffer ) ;
Return $ Buffer ;
}
Include ( " Style.css " ) ; // Include the files you want to compress here. Use include to include multiple files and sort them in sequence.
If ( Extension_loaded ( " Zlib " )){
Ob_end_flush () ; // Output buffer content
}
?>
Step 2: Call
Take this site as an example. The original CSS call is:
< Link REL = " Stylesheet " Media = " Screen " Href = " Http://mrthink.net/wp-content/themes/zsofa/style.css " />
After gzip compression, the call method is as follows:
< Link REL = " Stylesheet " Media = " Screen " Href = " Http://mrthink.net/wp-content/themes/zsofa/css.php? V = 100415 " />
Note that if you have multiple CSS calls before, you only need to call one after gzip. of course, the premise is that your compressed PHP file contains all the CSS style sheets to be called.
Step 3: compress and useArticleThe yslow plug-in mentioned at the beginning analyzes your website and compares the two scores. I believe there will be surprises.
Finally, please note that the cache time is set in the PHP file of gzip. If you change the CSS file, it does not take effect immediately after it is uploaded. The effective time is in the above gzip code.$ OffsetThe value set later. The solution is actually very simple. After modifying CSS, modify the style call path at the same time.... /CSS. php? V = 100415The value of 100415 (this value can be obtained at will.
The user experience is to start from the most detail, forget your own behavior model, and consider the user from the user's perspective, your website's user experience is half done.
Published in mr. Think personal blog: http://mrthink.net/ue-php-gzip-function/ reprint please note.