◆Compressed outputGzip
Using the mod_gzip module in Apache, we can use the gzip compression algorithm to compress the webpage content published by the Apache server and then transmit it to the browser of the client. If it is a plain text content, the effect is very obvious, it can be compressed to the original 30%-40%, so that the user's browsing speed is greatly accelerated.
Gzip requires support from client browsers. Most browsers currently support gzip, such as IE, Netscape, and Mozilla. Therefore, this method is worth a try. We can use the predefined variables in PHP.$ _ Server ['HTTP _ accept_encoding ']To determine whether the client browser supportsGzip.
Gzip1.php
if(
ereg
(
'gzip'
,
$_SERVER
[
'HTTP_ACCEPT_ENCODING'
])) {
???
//
Browser support
} else {
???
//
The browser does not support output of other content
}
?>
Next we will expand the above PHP program and use
Ob_start (ob_gzhandler) is used to compress the webpage content, store it in a buffer, and send it to a browser that supports gzip. the browser automatically decompress the Compressed Content and display it.
Gzip2.php
define
(
'MAX'
,
100
);
if(
ereg
(
'gzip'
,
$_SERVER
[
'HTTP_ACCEPT_ENCODING'
]))
{
???
//
Browser support
gzip
To compress the content and buffer the output.
????
ob_start
(
"ob_gzhandler"
);
????
$output
=
''
;
????for(
$i
=
0
;
$i
MAX
;
$i
++)
????{
????????
$output
.=
"This is line $i
"
;
????}
????echo
"
Browser support
gzip
Compressed output
"
;
????echo
$output
;
}
else
{
????
//
The browser does not support direct output.
????
for(
$i
=
0
;
$i
MAX
;
$i
++)
????{
????????
$output
.=
"This is line $i
"
;
????}
????
????echo
"
Browser not supported
gzip
Compressed output
"
;
????echo
$output
;
}
?>
?
The HTTP header information of a Web page generated by using gzip compression is more like this than that of a general web page:
Content-encoding: Gzip
Content-Length: 270
?
For more details, see the mod_gzip project homepage:
Http://sourceforge.net/projects/mod-gzip/
?
Similarly, we can also use mod_deflate to lower the compression ratio than mod_gzip. Calling the zip function consumes server memory, so use it with caution, depending on your needs.