Home  >  Article  >  Backend Development  >  PHP program acceleration exploration compressed output gzip_PHP tutorial

PHP program acceleration exploration compressed output gzip_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:34:401037browse

Using the mod_gzip module in Apache, we can use the gzip compression algorithm to compress the web page content published by the Apache server and then transmit it to the client's browser. If it is pure text content, the effect is very obvious, and it can be compressed to about 30%-40% of the original size, greatly speeding up the user's browsing speed.

Gzip requires client browser support. Currently, most browsers support gzip, such as IE, Netscape, Mozilla, etc., so this method is worth a try. We can use the predefined variable $_SERVER[‘HTTP_ACCEPT_ENCODING’] in PHP to determine whether the client browser supports gzip.

gzip1.php

if(ereg(gzip,$_SERVER[HTTP_ACCEPT_ENCODING])) {
if(ereg(gzip,$_SERVER[HTTP_ACCEPT_ENCODING])) {
 //浏览器支持
} else {
 //浏览器不支持,输出其它内容
}
?>
//Browser support
} else {
//The browser does not support it, output other content
}
?>

Next, we extend the above PHP program and use ob_start(ob_gzhandler) to compress the web page content, store it in the buffer and send it to a browser that supports gzip. The browser will automatically decompress and display the compressed content.
define(MAX,100);

if(ereg(gzip,$_SERVER[HTTP_ACCEPT_ENCODING]))
{
 //浏览器支持gzip,将内容压缩并缓冲输出
 ob_start("ob_gzhandler");
 $output = ;

 for($i=0;$i<=MAX;$i++)
 {
  $output .= "This is line $i ";
 }
 echo "浏览器支持gzip压缩输出";
 echo $output;
}
else
{
 //浏览器不支持,直接输出
 for($i=0;$i<=MAX;$i++)
 {
  $output .= "This is line $i ";
 }

 echo "浏览器不支持gzip压缩输出 ";
 echo $output;
}
?>

gzip2.php

define(MAX,100);
Content-Encoding: gzip
Content-Length: 270

if(ereg(gzip,$_SERVER[HTTP_ACCEPT_ENCODING]))
{
//The browser supports gzip, which compresses the content and buffers the output
ob_start("ob_gzhandler");
​$output = ;

​for($i=0;$i<=MAX;$i++)

{ } echo "The browser supports gzip compressed output"; echo $output; } else { //Browser does not support it, output directly ​for($i=0;$i<=MAX;$i++) { ​$output .= "This is line $i "; }
echo "The browser does not support gzip compressed output"; echo $output;
}
?>
Compared with ordinary web pages, the HTTP header information of web pages generated using gzip compression will have more information like this:
Content-Encoding: gzip Content-Length: 270
​If you want to get more detailed information, please see the mod_gzip project homepage: ​http://sourceforge.net/projects/mod-gzip/ Similarly, we can also use mod_deflate, the compression rate is slightly lower than mod_gzip. Calling the zip function requires server memory, so use it with caution and depending on your needs. http://www.bkjia.com/PHPjc/508468.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508468.htmlTechArticleUsing the mod_gzip module in Apache, we can use the gzip compression algorithm to compress the web content published by the Apache server and then transmitted to the client's browser. If it is plain text...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn