熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

PHP實現gzip頁面壓縮方法

2013-11-15 12:24:50  來源: PHP編程 

  示例一(用php的內置壓縮函數)

<?PHP 
if(Extension_Loaded(’zlib’)) Ob_Start(’ob_gzhandler’); 
Header("Contenttype: text/html"); 
?> 
<!DOCTYPE html PUBLIC "//WC//DTD XHTML  Transitional//EN" "
<html xmlns="
<head> 
<meta httpequiv="ContentType" content="text/html; charset=gb" /> 
<title>無標題文檔</title> 
</head> 
<body> 
<?php 
for($i=;$i<;$i++){ 
echo ’Hello World!’; 

?> 
</body> 
</html> 
<?PHP 
if(Extension_Loaded(’zlib’)) Ob_End_Flush(); 
?> 


示例二(自寫函數)

<?php ob_start(’ob_gzip’); ?> 

<!DOCTYPE html PUBLIC "//WC//DTD XHTML  Transitional//EN" "
<html xmlns="
<head> 
<meta httpequiv="ContentType" content="text/html; charset=gb" /> 
<title>無標題文檔</title> 
</head> 

<body> 
</body> 
</html> 

<?php 
ob_end_flush(); 
//壓縮函數 
function ob_gzip($content){ 
if(!headers_sent()&&extension_loaded("zlib")&&strstr($_SERVER["HTTP_ACCEPT_ENCODING"]"gzip")){ 
$content = gzencode($content); 
header("ContentEncoding: gzip"); 
header("Vary: AcceptEncoding"); 
header("ContentLength: "strlen($content)); 

return $content; 

?> 
 


From:http://tw.wingwit.com/Article/program/PHP/201311/20896.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.