PHP zlib Extension Implementation page gzip compression output _php skills

Source: Internet
Author: User
Tags strlen
To achieve the GZIP compression page requires a common browser and server support, is actually the server compression, uploaded to the browser after the browser decompression and resolution. Browsers don't need us to worry because most browsers now support parsing gzip-enabled pages. All we need to do is compress the page on the server side and then output it to the browser.

A little wordy, here's the thing:

Just as to make a compressed biscuit, first to get the raw material, to compress a page, first to get the content to output. The Ob_start () (OB => output buffer) function in PHP allows you to put the contents of the program's output into a place called a "buffer", which, of course, can be understood as a platform for making compressed cookies for temporary release.
This function must be used before the page output, so it is generally placed at the top of the code. Because it is like a workbench, so you have to be prepared before the arrival of raw materials, or raw materials come to no place to put, there will be problems. With Ob_start () to compress the page, we can make compression cookies, no, it should be able to compress the page! But there seems to be a lack of a compressor, EZ, we use PHP with the zlib expansion to do one:
Copy Code code as follows:

function Ob_gzip ($content)//$content is the content of the page to be compressed, or the cookie material.
{
if (!headers_sent () &&//If the page header information has not yet been exported
extension_loaded ("zlib") &&/and zlib extensions have been loaded into PHP
Strstr ($_server["http_accept_encoding"], "gzip")//and browser says it can accept gzip pages
{
$content = Gzencode ($content. "\n//This page has been compressed", 9); Post a "//this page compressed" comment label for the content being compressed, and then perform a 9-level compression with the Gzencode () function provided by zlib, which ranges from 0-9,0 to zero compression, and 9 for maximum compression, which, of course, costs more CPU.

Then use the header () function to send some header information to the browser, tell the browser this 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 cookie back to the workbench.
}

After the compressor is done, we put the compressor on the workbench, so the original Ob_start () becomes

Ob_start (' Ob_gzip '); Yes, is to give Ob_start () add a parameter, the parameter name is we just do the "compressor" function name. So when the content goes into the buffer, PHP calls the Ob_gzip function to compress it.
Well, all the work has been done, the final delivery:

Ob_end_flush (); End buffer, output content. Of course, this function is also OK, because the program will automatically output the buffer content at the end of execution.
The complete example is as follows:
Copy Code code as follows:

<?php
Enable a workbench with a ob_gzip compressor
Ob_start (' Ob_gzip ');
Prepare some content to be compressed
For ($i =0 $i <100; $i + +)
{
Echo (' Here is the raw material for compressing cookies, here is the raw material for compressing cookies ');
}
Output compression Results
Ob_end_flush ();
This is Ob_gzip compressor.
function Ob_gzip ($content)
{
if (!headers_sent () &&
extension_loaded ("zlib") &&
Strstr ($_server["http_accept_encoding"], "gzip")
{
$content = Gzencode ($content. "\n//This page has been compressed", 9);

Header ("Content-encoding:gzip");
Header ("vary:accept-encoding");
Header ("Content-length:". strlen ($content));
}
return $content;
}
?>

After the actual test, the above code if not gzip, is 4.69kb=4802.56b, enabling gzip reduced to 104B, er ... I may not be good at math, I have compressed to the original percentage of it.

In addition, the following is the log information obtained with FlashGet, you can see the header information in our program Riga:
Copy Code code as follows:

Fri 17:53:10 2008 http/1.1 OK
Fri 17:53:10 2008 server:microsoft-iis/5.1
Fri 17:53:10 2008 Date:fri, 2008 09:53:10 GMT
Fri 17:53:10 2008 Connection:close
Fri 17:53:10 2008 x-powered-by:php/5.2.5
Fri 17:53:10 2008 Content-encoding:gzip
Fri 17:53:10 2008 Vary:accept-encoding
Fri 17:53:10 2008 content-length:104
Fri 17:53:10 2008 Content-type:text/html

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.