PHPzlib extension page GZIP compression function details _ PHP Tutorial

Source: Internet
Author: User
Details about the GZIP compression function of the PHPzlib extension page. 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 the browser. to compress the GZIP page, the browser and the server need to support it. In fact, the server is compressed and uploaded to the browser. then, the browser decompress and parse the file. 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:

The code is as follows:

Function ob_gzip ($ content) // $ content is the page content to be compressed, or the biscuit raw material.
{
If (! Headers_sent () & // if the page header information has not been output
Extension_loaded ("zlib") & // and zlib extensions have been loaded into PHP
Strstr ($ _ SERVER ["HTTP_ACCEPT_ENCODING"], "gzip") // and the browser says it can accept GZIP pages
{
$ Content = gzencode ($ content. "// 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:

The code is as follows:

// Enable a workbench with an ob_gzip compressor
Ob_start (ob_gzip );
// Prepare some content to be compressed
For ($ I = 0; I I <100; $ I)
{
Echo (here is the raw material for compressing cookies, here is the raw material for compressing cookies );
}
// Output compressed results
Ob_end_flush ();
// This is the ob_gzip compressor.
Function ob_gzip ($ content)
{
If (! Headers_sent ()&&
Extension_loaded ("zlib ")&&
Strstr ($ _ SERVER ["HTTP_ACCEPT_ENCODING"], "gzip "))
{
$ Content = gzencode ($ content. "// This page is compressed", 9 );

Header ("Content-Encoding: gzip ");
Header ("Vary: Accept-Encoding ");
Header ("Content-Length:". strlen ($ content ));
}
Return $ content;
}
?>

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:

The code is as follows:

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

Bytes. We don't need to worry about the browser ,...

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.