PHP Open gzip Page Compression instance
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 "> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/> <title> Untitled Document </title> <body> <?php For ($i =0 $i <10000; $i + +) { Echo ' Hello world! '; } ?> </body> ? 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 "> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/> <title> Untitled Document </title>
<body> </body>
<?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; } ?> |