PHP gzip compression js with CSS code example

Source: Internet
Author: User
In order to save bandwidth, we often need to compress the file after transmission, in PHP programming, the most common is to use the Gzip module to compress CSS and JS files, this article for everyone to give an example of this, for your reference.

First, to understand the advantages of compression: 1), merging multiple CSS/JS documents into one file to reduce HTTP requests. 2), the merged files for document compression, such as the use of JS compressor and CSS compress respectively. 3), if you use some mainstream JavaScript frameworks, such as jquery, MooTools or Yui, it is highly recommended to use the Google AJAX Library as an external link to import the base library directly. 4), use gzip to compress the JS/CSS document on the server side.

Using gzip compression with PHP is very simple and its core is to use Ob_gzhandler. Note: Not all browsers support gzip transfer of data to the client, so fault-tolerant processing must be taken into account. For everyone to give a PHP compression CSS by gzip example. In the folder where you placed the CSS, create a new style.php file. File style.php:

 
      

The above code is only used to compress the CSS file, if you need to compress the JavaScript file, the above code in the 5th line of Content-type modified to:

Header (' Content-type:application/x-javascript; charset:gb2312 ');

At the same time to pay attention to the file encoding, here is gb2312, if you are using UTF-8 or other code, modify the corresponding (that is, utf-8) can be. After the modification is completed, introduce the relevant required CSS files, and then replace the original HTML into CSS with the following introduction:

 
      

The same way JS is introduced as follows:

Because the expires (out-of-date) property used in the code above is used to cache the CSS/JS code on the client, if the expiration time is set too long (for example, one year), the client may not take effect immediately when you modify the JS/CSS code on the server side. Workaround: Add a random parameter to the PHP file, such as the v=121 in the example above, and when you modify the file next time, remember to modify this random parameter (such as change to 122).

This is it, I hope that you do a test, to see the compression before and after the site access speed, there will be a big change! Programmer's Home, I wish you all study progress, day up, upward!!

  • 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.