Home > php教程 > php手册 > PHP实现gzip页面压缩方法

PHP实现gzip页面压缩方法

WBOY
Release: 2016-06-13 11:35:28
Original
751 people have browsed it

示例一(用php的内置压缩函数):

if(Extension_Loaded(’zlib’)) Ob_Start(’ob_gzhandler’); 
Header("Content-type: text/html"); 
?> 
nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 

 
 
无标题文档 
 
 
for($i=0;$i echo ’Hello World!’; 

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


示例二(自写函数):

 

nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
 
 
无标题文档 
 

 
 
 

ob_end_flush(); 
//压缩函数 
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; 

?> 
 
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template