Example of phpgzip compression js and css code

Source: Internet
Author: User
Example of phpgzip compression js and css code
To save bandwidth, we often need to compress and transmit files. in php programming, the most common is to use the gzip module to compress css and js files, this article provides an example for your reference.

First, let's take a look at the advantages of compression: 1) combine multiple CSS/JS documents into one file to reduce HTTP requests. 2) compress the merged files, such as JavaScript compressor and CSS compress. 3) if some mainstream JavaScript frameworks such as JQuery, Mootools or YUI are used, we strongly recommend that you directly import the Base Library using links other than Google AJAX Library. 4) use GZIP to compress JS/CSS files on the server.

Using GZIP in PHP is very simple, and the core is ob_gzhandler. Note: not all browsers support GZIP data transmitted to the client. fault tolerance processing must be taken into account. Here is an example of compressing CSS with GZIP in PHP. In the folder where CSS is stored, create a new style. php file. File style. php:

     

The above code is only used to compress css files. to compress JavaScript files, modify the Content-type of line 5th in the above code:

header('content-type:application/x-javascript; charset: gb2312');

At the same time, pay attention to the file encoding, here is the gb2312, if you use a UTF-8 or other encoding, modify to the corresponding (that is, UTF-8. After the modification is complete, introduce the required CSS file, and then replace the original HTML with the following introduction method:

     

Similarly, JS is introduced as follows:


    

Because the above code uses the HTTP Expires (expiration) attribute to cache CSS/JS code on the client, if the expiration time is set too long (for example, one year ), when you modify JS/css code on the server side, the client may not take effect immediately. Solution: add a random parameter after the php file, such as v = 121 in the above example. when you modify the file next time, remember to modify the random parameter (for example, change it to 122) accordingly.

That's all. I hope you can test it yourself to see if the website access speed before and after compression has greatly improved! Programmer's house. I wish you all the learning and progress every day !!

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.