Compress Your PHP, JS, CSS, and other text-type code

Source: Internet
Author: User

As we all know, reducing the size of code during transmission can increase the page transmission speed and speed up opening the page. This is based on the bandwidth bottleneck theory. Of course, the server speed is affected by many other factors, there are PHP code execution speed, database access speed, disk read/write IO speed, etc. In any case, the bandwidth impact is very large, however, due to the constraints of China Telecom or China Netcom and other telecom companies, compression code transmission will greatly save bandwidth when the server network bandwidth is insufficient!

This article describes how to use Gzip to automatically compress the corresponding code. On the visitor's browser, the code is automatically restored to allow the browser to execute the code, the space-saving method with good style during code writing is not listed in this article.

GZIP was first created by Jean-loup Gailly and Mark Adler for File compression in UNIX systems. We often use files suffixed with .gz in linux, which are in GZIP format. Nowadays, it has become a widely used data compression format on the Internet, or a file format. GZIP encoding on HTTP is a technology used to improve the performance of WEB applications. Large-Traffic WEB sites often use GZIP compression technology to make users feel faster.

First, test whether each page of your website adopts Gzip compression. Here is a test tool.

Currently, there are two mainstream methods for Gzip compression.
I. mod_deflate module

The mod_deflate module provides the DEFLATE output filter, which allows the server to compress the output content before sending it to the client (gzip data format should also be used for compression, and deflate is a compression algorithm) to save bandwidth. In Apache 1.3.x era, the mod_gzip module is used for implementation. Apache2 has built-in mod_deflate module.

This method requires that Apache's http server software is installed and the deflate module is loaded.

Usage:

Add:

  1. <Ifmodule mod_deflate.c>
  2. AddOutputFilter DEFLATE html htm xml php js css
  3. </Ifmodule>

In this way, Apache will use Gzip to transmit data when publishing files such as html, xml, php, js, and css. This method is simple and permanent, however, the deflate module is not installed on the virtual host in general, and it is not necessarily Apache. However, we still have the following methods:

2. ob_gzhandler Function
Host Requirements: PHP is supported, PHP is later than 4.0.4, and zlib library is installed.

For PHP File compression during transmission, you only need to add the first line of the original PHP File


 
<? Php if (extension_loaded ('zlib ') {ob_start ('ob _ gzhandler') ;}?>
And add
 
<? Php if (extension_loaded ('zlib ') {ob_end_flush () ;}?>
Copy to the regular PHP format.
 
For js files, add
 
<? Php if (extension_loaded ('zlib ') {ob_start ('ob _ gzhandler');} header ("Content-Type: text/javascript");?>
Add
 
<? Php if (extension_loaded ('zlib ') {ob_end_flush () ;}?>
Save it as a file like abc. js. PHP, and modify and direct it to all html, PHP, and other files.
 
Change
 
<Script type = "text/javascript" src = "abc. js" charset = "UTF-8"> </script>
Is
 
<Script type = "text/javascript" src = "abc. js. php" charset = "UTF-8"> </script>
For the.html file, you can directly change it to. PHP has been accessed. If you want to retain the original URL, modify it. you can use the htaccess pseudo URL or create a webpage to jump to the page. Other CSS and other texts can be used to calculate how to change the URL. Compression Efficiency Description: the source code of the jQuery library is about 50 k. It is optimized when the code is written, and the space combination function is removed to reduce it to 26 k, and then compressed to 14 k using GZIP!


The following is a comparison of the compression efficiency of other websites. The three methods in the figure are GZIP, but the execution methods are different.

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.