压缩php构建的网站: 部署和测试

本文主要介绍如何在php网页中使用ob_start()和ob_gzhandler()函数压缩网页, 并测试压缩效果.

1. 准备

首先查看php是否支持zlib module, 检查phpinfo()信息或者拷贝下面代码到info.php中:

[code]
phpinfo();
?>
[/code]

然后把info.php上传到您的web服务器上, 打开您的浏览器, 访问info.php网页, 查看zlib部分. 如果zlib已安装, 如下图所示:

另外注意服务器上的PHP版本要在4.0.6或者以上版本, 否则您将不能成功使用本文的方法成功压缩PHP网页.

2. 部署

你仅需要拷贝下面代码到test.php中, 然后上传到web服务器上:
[code]
ob_start( 'ob_gzhandler' );
?>



网页压缩成功!



[/code]
实例(压缩):
http://islab.org/test/080223/test.php

实例(未压缩):
http://islab.org/test/080223/test_uncompress.php

3. 测试

实例(压缩):
http://islab.org/test/080223/test.php

测试结果:
URL Compression Report

URL Downloaded:
http://islab.org/test/080223/test.php

Document Status: Compressed

Compressed File Size: 79
Is PipeBoost Customer: NO
File Is already being served by HTTP Compression Software. No analysis has been done.
Original File Size: 67
Original File Size Reduction: 8.96%

Header Value
Date Sat, 23 Feb 2008 13:48:01 GMT
Server Apache
Content-Encoding gzip
Vary Accept-Encoding
Keep-Alive timeout=15, max=100
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type text/html

实例(未压缩):
http://islab.org/test/080223/test_uncompress.php

URL Downloaded:
http://islab.org/test/080223/test_uncomp
ress.php

Document Status: Uncompressed

Uncompressed File Size: 63
Compressed File Size: 68
Compression CPU Time Consumption: < 10 ms.
Compression Ratio: 9 - Highest
*File Size Reduction: -7.94%
Downloads Speed Improvement: -7%
*Please note, that URL Compression Report will not process frames, entry redirect pages, Java Script includes and Style Sheets. Please specify exact URL location if you want to get very specific results.

Downloaded HTTP Headers:

Header Value
Date Sat, 23 Feb 2008 13:50:13 GMT
Server Apache
Keep-Alive timeout=15, max=100
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type text/html

4. 小结
1). 从上面测试结果我们可以看到采用压缩后的PHP网页, 大小由原来的79字节减少到67字节.
2). 我们访问压缩后的PHP网页, 访问速度有所提高, 从而也减少了总的数据流量.
3). 由于图片本身是经过压缩的(比如gif, jpg, 等等), 该方法对图片不会有任何影响.

Keyword: , , ,