ob_start();//打开输出缓冲
ob_implicit_flush(0);//
//*****************************************************************//
//函数名:canGzip()
//作用:检查客户浏览器是否支持gzip,x-gzip编码
//参数:
//返回值:支持的编码类型"gzip", "x-gzip", 返回false代表不支持
//*****************************************************************//
function canGzip()
{
//if (headers_sent() || connection_status)
//return false;
if (strpos('King'.$_SERVER["HTTP_ACCEPT_ENCODING"], 'gzip') !== false)
return "gzip";
if (strpos('King'.$_SERVER["HTTP_ACCEPT_ENCODING"], 'x-gzip') !== false)
return "x-gzip";
return false;
}
//*****************************************************************//
//函数名:doGzipOut($level, $debug)
//作用:对输出缓冲的数据进行压缩并输出
//参数:$level代表压缩级别, 0 = 不压缩, 9 = 最大压缩率
// $debug代表是否输出调试信息, 1 = 输出, 0 = 不输出
//返回值:
//*****************************************************************//
function doGzipOut($level = 1, $debug = 0)
{
$ENCODING = canGzip();
if ($ENCODING)
{
echo "n<!-- Use compress $ENCODING --n";
$contents = ob_get_contents();
ob_end_clean();
if ($debug)
{
$s = "<pNot compress length: ".strlen($contents);
$s .= "<br/Compressed length: ".strlen(gzcompress($contents,$level));
$contents .= $s;
}
header("Content-Encoding: $ENCODING");
echo "x1fx8bx08x00x00x00x00x00"; //???
$size = strlen($contents);
$crc = crc32($contents);
$contents = gzcompress($contents, $level);
$contents = substr($contents, 0, strlen($contents) - 4); //???
echo $contents;
echo pack('V',$crc);
echo pack('V',$size);
exit;
}
else
{
ob_end_flush();
exit();
}
}
?
使用方法: ------------Start of file----------
|< ?
| include('gzipOut.php');
|?
|<HTML
|... the page ...
|</HTML
|< ?
| echo "............"
|
| doGzipout();
|?
-------------End of file-----------