PHP uses zlib extension to implement page gzip compression output

Source: Internet
Author: User
Tags implement strlen

GZIP (Gnu-zip) is a compression technique. The page size can be turned into 30% or smaller after gzip compression. This way the user will feel very happy when browsing!

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:

function ob_gzip($content) // $content 就是要压缩的页面内容,或者说饼干原料
{
  if( !headers_sent() && // 如果页面头部信息还没有输出
    extension_loaded("zlib") && // 而且zlib扩展已经加载到PHP中
    strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip")) //而且浏览器说它可以接受GZIP的页面
  {
    $content = gzencode($content."\n//此页已压缩",9); 为准备压缩的内容贴上“//此页已压缩”的注释标签,然后用zlib提供的gzencode()函数执行级别为9的压缩,这个参数值范围是0-9,0表示无压缩,9表示最大压缩,当然压缩程度越高越费CPU。
    
    //然后用header()函数给浏览器发送一些头部信息,告诉浏览器这个页面已经用GZIP压缩过了!
    header("Content-Encoding: gzip");
    header("Vary: Accept-Encoding");
    header("Content-Length: ".strlen($content));
  }
  return $content; //返回压缩的内容,或者说把压缩好的饼干送回工作台。
}

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:

<?php
//启用一个带有ob_gzip压缩机的工作台
ob_start('ob_gzip');
//准备一些待压缩的内容
for($i=0; $i<100; $i++)
{
  echo('这里是压缩饼干的原料,这里是压缩饼干的原料,原料');
}
//输出压缩成果
ob_end_flush();
//这是ob_gzip压缩机
function ob_gzip($content)
{
  if(  !headers_sent() &&
    extension_loaded("zlib") &&
    strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip"))
  {
    $content = gzencode($content."\n//此页已压缩",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:

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.