Home  >  Article  >  php教程  >  加速你的页面--数据压缩

加速你的页面--数据压缩

WBOY
WBOYOriginal
2016-06-13 11:21:471211browse

最近偶的网站越来越慢,不是脚本执行时间慢,是网络传输速度慢。
知道http1.1支持gzip编码的数据,所以试试将自己的页面压缩压缩...
(在php.ini可以设置,直接输出为gzip编码,不过没试过)

上网搜到一个gzdoc.php,改了改,让大家共同琢磨琢磨。
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 "nn";
$contents = ob_get_contents();


Statement:
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
Previous article:把静态变量作为Cache使用Next article:php压缩技术