文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>实例解说PHP动态内容的缓冲和压缩

实例解说PHP动态内容的缓冲和压缩

时间:2007-02-17  来源:PHP爱好者

PHP4 采用了缓冲机制,在你决定发送以前,所有内容只是存在于缓冲中,而不是直接发送给浏览器,虽然你可以用 header 和 setcookie 函数来实现,但是这两个函数相比于功能强大的输出函数来说只是一点“雕虫小技”。让我们来看看这些函数的真本事:
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
void ob_start(void);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
本函数告诉 PHP 处理器把所有输出重定向到内部缓冲,调用这个函数后,就不会有输出到浏览器。
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
string ob_get_contents(void);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
本函数把输出缓冲返回到一个字符串,你可以用来把堆积起来的输出一起发送到浏览器。当然要先关掉缓冲。
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
int ob_get_length(void);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
本函数返回输出缓冲的长度。
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
void ob_end_clean(void);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
本函数清除并关闭缓冲。在输出到浏览器之前你需要使用这个函数。
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
void ob_implicit_flush ([int flag])
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
本函数用来控制隐式缓冲泻出,缺省为 off,如果打开时,对每个 print/echo 或者输出命令的结果都发送到浏览器。
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
二、采用输出控制来压缩 PHP 的输出
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
在开始之前,要保证你的 PHP4 编译时支持 Zlib。
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
首先,初始化输出缓冲:
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
ob_start();
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
ob_implicit_flush(0);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
?>
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
然后产生所有的输出内容。
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
print("本例为压缩输出!");
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
?>
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
页面生成以后,采用:
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
$contents = ob_get_contents();
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
ob_end_clean();
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
?>
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
还要检查浏览器是否支持压缩数据,我们采用在变量 $HTTP_ACCEPT_ENCODING 中检查 "gzip, deflate"的办法:
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
if(ereg(’gzip, deflate’,$HTTP_ACCEPT_ENCODING)) {
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
// 产生 gzip 后的内容
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
} else {
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
echo $contents;
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
}
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
?>
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
下面我们分析怎样产生 gzip 输出:
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
// 告诉浏览器下面接收的是 gzip 数据。
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
header("Content-Encoding: gzip");
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
// 显示 gzip 文件的文件头
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
// 只要一次就够了
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
echo "x1fx8bx08x00x00x00x00x00";
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
// 计算长度和 CRC 校验码
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
$Size = strlen($contents);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
$Crc = crc32($contents);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
// 压缩数据
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
$contents = gzcompress($contents, 9);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
// 不能直接在这里输出内容,因为还没有写入 CRC 呢!
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
$contents = substr($contents, 0, strlen($contents) - 4);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
echo $contents;
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
gzip_PrintFourChars($Crc);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
gzip_PrintFourChars($Size);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
function gzip_PrintFourChars($Val) {
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
for ($i = 0; $i <4; $i ++) {
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
echo chr($Val % 256);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
$Val = floor($Val / 256);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
}
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
}
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
?>
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
三、缓冲 PHP 的输出
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
在 PHP4 里能很容易的实现缓冲,我们来看例子:
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
// 对请求的 URI 产生唯一的文件名。
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
$cached_file=md5($REQUEST_URI);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
if((!file_exists("/cache/$cached_file"))||(!is_valid("/cache/$cached_file"))) {
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
ob_start();
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
ob_implicit_flush(0);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
// 在这里输出缓冲
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
$contents = ob_get_contents();
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
ob_end_clean();
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
$fil=fopen($cached_file,"w+");
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
fwrite($fil,$contents,$strlen($contents));
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
fclose($fil);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
}
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
readfile($cached_file);
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
?>
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
四、结论
www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U

www.chinaitpower.com5VgdMdqqwUlpyjMRACmnWAA2U
PHP 输出缓冲函数在操作脚本输出方面十分有用,把缓冲压缩后输出能减少 80% 的输出时间,这对于存取其他数据资源(例如数据库或者 XML)来说,也是一个很好的缓冲机制。
php爱好者站 http://www.phpfans.net c/vc/c++/java.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载