Home >Backend Development >PHP Tutorial >Compress your PHP, JS, CSS and other text type codes_PHP tutorial
As we all know, reducing the size of the code during transmission can increase the transmission speed of the page and open the web page faster. This is based on the theory of bandwidth bottleneck. Of course, the server speed is affected by many other factors. There are PHP codes Execution speed, database access speed, disk read and write IO speed, etc., no matter what, the impact on bandwidth is very large. However, due to the constraints of China Telecom, China Netcom and other telecommunications companies, server network bandwidth is not very abundant. Under certain conditions, compressed code transmission will greatly save bandwidth!
This article describes the use of Gzip compression to automatically compress the corresponding code, which will be automatically restored to code on the visitor's browser for browser execution. As for good style and space-saving methods when writing code, this article is not included in this article.
GZIP was first created by Jean-loup Gailly and Mark Adler for file compression on UNIX systems. We often use files with the suffix .gz in LINUX, and they are in GZIP format. Nowadays, it has become a very common data compression format, or file format, used on the Internet. GZIP encoding over HTTP protocol is a technology used to improve the performance of WEB applications. High-traffic WEB sites often use GZIP compression technology to allow users to experience faster speeds.
First test whether each page of your website uses Gzip compression. Here is a test tool
Currently, there are two mainstream methods to implement Gzip compression.
1. 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 (it should also use gzip data format compression, deflate is a compression algorithm) to save bandwidth. The Apache 1.3.x era uses the mod_gzip module to implement this, and Apache2 already has a built-in mod_deflate module.
This method requires Apache http server software to be installed and the deflate module loaded.
How to use:
Add in the .htaccess file in the web publishing directory:
This will cause Apache to use Gzip to transmit data when publishing files such as html, xml, php, js, css. This method is simple and can be done once and for all, but the generally used virtual hosts do not have the deflate module installed, nor do they It must be from Apache, but we still have a way:
2. ob_gzhandler function
Host requirements: support PHP, PHP version is 4.0.4 or above, and the zlib library
For compression during PHP file transfer, just add
to the first line of the original PHP file
And add
at the end
Restore to regular PHP format when copying.
For js files, add
to the first line of the original js file
Also add
at the end
Then save it as a file like abc.js.PHP, and modify all html, PHP and other files to point to this.
Change
for
For the a.html file, just change it to a.PHP for access. If you want to keep the original URL, modify the .htaccess pseudo URL or create a web page jump, and other CSS and other texts can be deduced based on this. Changed. Compression performance description: The jQuery library source code is about 50k. It is optimized when writing the code first, removing spaces, combining functions, etc. to reduce it to 26k, and then using GZIP to compress it to 14k!
Attached is a comparison chart of compression performance of other websites. The three methods in the picture are all GZIP, but the implementation methods are different.