Home >Backend Development >PHP Tutorial >PHP implementation of gzip page compression method_PHP tutorial

PHP implementation of gzip page compression method_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:52:49758browse

Example 1 (using PHP’s built-in compression function):

if(Extension_Loaded(’zlib’)) Ob_Start(’ob_gzhandler’);
Header("Content-type: text/html");
?>




Untitled Document


for($i=0;$i<10000;$i++){
echo ‘Hello World!’;
}
?>


if(Extension_Loaded(’zlib’)) Ob_End_Flush();
?>


Example 2 (self-written function):







Untitled Document






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;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371540.htmlTechArticleExample 1 (using PHP’s built-in compression function): ?PHP if(Extension_Loaded(zlib))Ob_Start(ob_gzhandler) ; Header(Content-type:text/html); ? !DOCTYPEhtmlPUBLIC-//W3C//DTDXHTML1.0Transiti...
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