Compressing your PHP, JS, CSS, and other text-type code _ PHP Tutorial

Source: Internet
Author: User
Compress your PHP, JS, CSS, and other text code. As we all know, by reducing the size of code during transmission, we can increase the page transmission speed and speed up webpage opening. this is based on the bandwidth bottleneck theory. of course, server speed is well known by others, by reducing the size of code during transmission, the page transmission speed can be improved and the webpage can be opened quickly. 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. AddOutputFilter DEFLATE html htm xml php js css

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




And add


Copy to the regular PHP format.

For js files, add


Add


Save it as a file like abc. js. PHP, and modify and direct it to all html, PHP, and other files.

Change

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.