PHP uses zlib extension to implement GZIP compressed page output

Source: Internet
Author: User
GZIP (GNU-ZIP) is a compression technology. After GZIP compression, the page size can be changed to 30% or even smaller. In this way, users will feel very happy when browsing! To implement GZIP compression pages, the browser and the server must support them together. actually, GNU-ZIP is a compression technology. After GZIP compression, the page size can be changed to 30% or even smaller. In this way, users will feel very happy when browsing!

To implement GZIP compression pages, the browser and the server need to support them. in fact, the server is compressed. after being uploaded to the browser, the browser decompress and parse the files. We don't need to worry about browsers, because most browsers now support parsing GZIP pages. We only need to compress the page on the server and then output it to the browser.

A little wordy. let's talk about the things below:

For example, to create a compressed biscuit, you must first obtain the raw material and compress a page. First, you must obtain the output content. The ob_start () (ob => output buffer) function in PHP can implement this function. it can put the content prepared for output in the program in a place called "buffer". of course, you can think of it as the workbench for temporarily storing raw materials for making compressed biscuits.

This function must be used before page output, so it is generally placed at the top of the code. Because it is like a workbench, you have to prepare the raw materials before they arrive. Otherwise, there will be problems when the raw materials arrive. After we use ob_start () to get the page to be compressed, we can make the compressed biscuit. no, it should be possible to compress the page! However, it seems that there is still a lack of compressor, EZ. We use the zlib extension of PHP to create a compressor:

Function ob_gzip ($ content) // $ content is the page content to be compressed, or the biscuit material {if (! Headers_sent () & // if the page header information has not been output extension_loaded ("zlib ") & amp; // and zlib extensions have been loaded to strstr ($ _ SERVER ["HTTP_ACCEPT_ENCODING"], "gzip") in PHP ")) // and the browser says it can accept the GZIP page {$ content = gzencode ($ content. "n // This page has been compressed", 9); paste the "// This page has been compressed" annotation label for the content to be compressed, and then use the gzencode () provided by zlib () the function execution level is 9. the value range of this parameter is 0-9. 0 indicates no compression, and 9 indicates the maximum compression. of course, the higher the compression level, the higher the CPU cost. // Then use the header () function to send some header information to the browser, telling the browser that the page has been compressed with GZIP! Header ("Content-Encoding: gzip"); header ("Vary: Accept-Encoding"); header ("Content-Length :". strlen ($ content);} return $ content; // return the compressed content, or send the compressed biscuit back to the workbench .}

After the compressor is ready, we place the compressor on the workbench, so the original ob_start () becomes

Ob_start ('OB _ gzip '); // Yes, that is, add a parameter to ob_start (). the parameter name is the name of the "Compressor" function we just created. In this way, when the content enters the buffer, PHP will call the ob_gzip function to compress it.

All right, all the work has been completed, and the final delivery is:

Ob_end_flush (); // end the buffer and output the content. Of course, this function is fine, because the buffer content is automatically output after the program is executed.

The complete example is as follows:

 

After actual tests, if GZIP is not used in the above code, it is 4.69KB = 4802.56B. after GZIP is enabled, it will be reduced to 104B ...... I may not be good at mathematics. I want to compress it to the original percentage ..

In addition, the following is the log information obtained with FlashGet. we can see the header information added to our program:

QUOTE:

Fri Jan 25 17:53:10 2008 HTTP/1.1 200 OK

Fri Jan 25 17:53:10 2008 Server: Microsoft-IIS/5.1

Fri Jan 25 17:53:10 2008 Date: Fri, 25 Jan 2008 09:53:10 GMT

Fri Jan 25 17:53:10 2008 Connection: close

Fri Jan 25 17:53:10 2008 X-Powered-By: PHP/5.2.5

Fri Jan 25 17:53:10 2008 Content-Encoding: gzip

Fri Jan 25 17:53:10 2008 Vary: Accept-Encoding

Fri Jan 25 17:53:10 2008 Content-Length: 104

Fri Jan 25 17:53:10 2008 Content-type: text/html


Address:

Reprinted at will, but please attach the article address :-)

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.