Example one (with PHP's built-in compression function):
;? PHP
if (extension_loaded (' zlib ')) Ob_start (' Ob_gzhandler ');
Header ("content-type:text/html");
?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-type" content= "text/html"; charset=gb2312 "/>
<title> Untitled Document </title>
</head>
<body>
<?php
for ($i =0 $i <10000; $i + +) {
Echo ' Hello world! ';
}
?>
</body>
</html>
;? PHP
if (extension_loaded (' zlib ')) Ob_end_flush ();
?>
Example Two (self-write function):
<?php ob_start (' Ob_gzip ');?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-type" content= "text/html"; charset=gb2312 "/>
<title> Untitled Document </title>
</head>
<body>
</body>
</html>
<?php
Ob_end_flush ();
//Compression function
function Ob_gzip ($content) {
if (!headers_sent () &&extension_loaded ("Zlib") &&strstr ($_server["http_accept_encoding"], " Gzip ")) {
$content = Gzencode ($content, 9);
header ("Content-encoding:gzip");
header ("vary:accept-encoding");
header ("Content-length:". strlen ($content));
}
return $content;
}
?>