关闭缓冲后再输出为什么没输出内容了?

在调用函数里使用ob_end_clean();可是随后echo $errorString却没有输出到浏览器.是哪里出问题?还是我函数用得不对?
部分代码:

 1   
 2  
 3ob_start("ob_gzhandler");   
 4  
 5...   
 6  
 7function errorHandler($errno, $errstr, $errfile, $errline)   
 8{   
 9switch ($errno):   
10case E_USER_NOTICE:   
11case E_USER_WARNING:   
12case E_WARNING:   
13case E_NOTICE:   
14case E_CORE_WARNING:   
15case E_CORE_ERROR:   
16case E_COMPILE_WARNING:   
17case E_USER_ERROR:   
18case E_ERROR:   
19case E_PARSE:   
20case E_CORE_ERROR:   
21case E_COMPILE_ERROR:   
22ob_end_clean();   
23$errorString = "错误 $errstr (错误号 $errno).

<br/>

 1\n文件 $errfile 第 $errline 行出错";   
 2echo $errorString;   
 3exit();   
 4default:break;   
 5endswitch;   
 6}   
 7  
 8// 执行错误处理函数   
 9set_error_handler("errorHandler");   
10  
11...   
12  
13ob_end_flush();   
14  

---------------------------------------------------------------

ob_end_clean
(PHP 4 )

ob_end_clean -- Clean (erase) the output buffer and turn off output buffering
Description
void ob_end_clean ( void)

This function discards the contents of the output buffer and turns off output buffering.

“and turn off output buffering”输出缓冲都关闭了,你怎么可能还输出呢?
---------------------------------------------------------------

1<html>
2<head>
3<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
4</head>
5<body>   

ob_start("ob_gzhandler");

echo "该处内容被输出到缓冲区<br/>\n";

function errorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno)
{
case E_USER_NOTICE:
case E_USER_WARNING:
case E_WARNING:
case E_NOTICE:
// case E_CORE_WARNING:
// case E_CORE_ERROR:
// case E_COMPILE_WARNING:
case E_USER_ERROR:
// case E_ERROR:
// case E_PARSE:
// case E_COMPILE_ERROR:
{
ob_end_clean();
$errorString = "错误 $errstr (错误号 $errno).<br/>\n文件 $errfile 第 $errline 行出错<br/>";
echo $errorString;
exit();
}
default:
break;
}
}

// 执行错误处理函数

set_error_handler("errorHandler");

//验证一下
$a = bbb; //这里出错.

ob_end_flush();

//执行结果略.

1  
2</body>
3</html>

---------------------------------------------------------------

是的,ob_end_clean清空和结束函数之前的缓冲,和后续的输出无关
但是请注意你打开输出管理的方式:ob_start("ob_gzhandler");
以这种方式打开的输出管理,关闭输出管理时都会有数据类型声明输出,哪怕内容为空。
于是你的程序将会出现在同一个http连接中输出两种类型的数据,这显然是错误的。
解决你的问题的办法是自行进行压缩输出,而不由ob_gzhandler自动完成
这也就是尽管从一开始php就提供ob_start("ob_gzhandler"),而仍有人在开发“压缩输出”函数的原因。其实你直接套用pear的cache类就可以了

Published At
Categories with Web编程
Tagged with
comments powered by Disqus