How does PHP enable compression and output for websites to increase website access speed?

Source: Internet
Author: User

Share two ways to compress page output data to speed up webpage access, one is to use php zlib extension to implement gzip compressed page output (Use php's built-in function ob_gzhandler for gzip compressed output), and the other is to modify php. enable php gzip in the ini configuration file to compress and transfer page information. Now let's take a look at how these two methods are implemented!

(1) compress gzip output using the php built-in function ob_gzhandler

Add the following code to the page header:

If (extension_loaded ('zlib ') & strstr ($ _ SERVER ['HTTP _ ACCEPT_ENCODING'], 'gzip ') {ob_start ('ob _ gzhandler ');} else {exit ('php zlib usage failed, please confirm whether zlib extension is loaded ');}

Add the following code at the end of the page:

if(extension_loaded('zlib')){ob_end_flush();}

The following are two websites with different domain names but identical information. The results are as follows:

(2) enable or disable php gzip by modifying the php. ini configuration file to compress and transmit page information
 
1. Find and open the php. ini configuration file, find zlib. output_compression = Off, and set

zlib.output_compression = Off   ;zlib.output_compression_level = -1

To:

zlib.output_compression = On   zlib.output_compression_level = 6

Php gzip configuration knowledge point:

1. By default, php does not enable zlib for full-site compression output. Instead, it uses the ob_gzhandler function for the page for compression output. You can only select either of the two. Otherwise, an error is reported.
 
2. The default value of zlib. output_compression is Off. You can set it to On or output buffer size (4 k by default)
 
3. zlib. output_compression_level indicates the compression ratio. The default compression ratio is 6. The optional range is 1-9.-1 indicates disabling php zlib (gzip) compression.
 
2. Save the php. ini configuration file and restart the apache server.
 
3. Open the apache configuration file httpd. conf and configure the deflate_module to be loaded. This step is the most critical step to enable php gzip compression output configuration.

#LoadModule deflate_module modules/mod_deflate.so

Remove the starting # And then restart apache.

Note: Compared with php version 4.3 or later, zlib must be separately installed before php4.3. In addition, I am using the php server built by phpnow to directly modify php. ini (phpnow php related options configuration file is php-apache2handler.ini) related configuration and restart apache on the line, do not need the second step operation.

The following are two websites with different domain names but identical information. The results are as follows:

 

You can view the specific effect in Firefox. Right-click the webpage and choose view page information option in the pop-up menu. In the pop-up window, check the size. My website has not implemented the function of modification. Sadly, my space does not support the gzip compression function, and the Code cannot be implemented!

Articles you may be interested in
  • Use the GZip compression function of PHP to compress website JS and CSS files to accelerate Website access.
  • How to Use zlib to compress output content to speed up web page opening
  • Add a favorites icon to a website
  • PHP compresses html webpage code to reduce the amount of network data transmitted, clear spaces, tabs, and comment mark
  • Shoulder, back muscle group exercise-Bring Up-bend up rowing-Move up-Stand Up-Stand up weight bend up-Sit posture neck pull down-bend upright hold up-neck front wide hold up
  • It is dangerous for multiple URLs on the website to access the same content.
  • How to optimize website pages and increase webpage loading speed
  • MySQL reference architecture from small websites to ultra-large scale websites

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.