使用php4加速网络传输


Posted in PHP onOctober 09, 2006

标 题: 使用php4加速网络传输。

<?
/***************************************
** Title.........: PHP4 HTTP Compression Speeds up the Web
** Version.......: 1.10
** Author........: catoc <catoc@163.net>
** Filename......: gzdoc.php
** Last changed..: 25/08/2000
** Requirments...: PHP4 >= 4.0.1
**                 PHP was configured with --with-zlib[=DIR]
** Notes.........: Dynamic Content Acceleration compresses
**                 the data transmission data on the fly
**                 code by sun jin hu (catoc) <catoc@163.net>
**                 Most newer browsers since 1998/1999 have
**                 been equipped to support the HTTP 1.1
**                 standard known as "content-encoding."
**                 Essentially the browser indicates to the
**                 server that it can accept "content encoding"
**                 and if the server is capable it will then
**                 compress the data and transmit it. The
**                 browser decompresses it and then renders
**                 the page.
** Useage........:
**                 No space before the beginning of the first '<?' tag
.
**                 ------------Start of file----------
**                 |<?
**                 | include('gzdoc.php');
**                 | print "Start output !!";
**                 |?>
**                 |<HTML>
**                 |... the page ...
**                 |</HTML>
**                 |<?
**                 | gzdocout();
**                 |?>
**                 -------------End of file-----------
***************************************/
ob_start();
ob_implicit_flush(0);
function GetHeader(){
        $headers = getallheaders();
        while (list($header, $value) = each($headers)) {
                $Message .= "$header: $value<br> ";
        }
        return $Message;
}
function CheckCanGzip(){
        global $HTTP_ACCEPT_ENCODING, $PHP_SELF, $Wget, $REMOTE_ADDR,  
$S_UserName;
        if (connection_timeout() || connection_aborted()){
                return 0;
        }
        if ((strpos('catoc'.$HTTP_ACCEPT_ENCODING, 'gzip')) || $Wget =
= 'Y'){
                if (strpos('catoc'.$HTTP_ACCEPT_ENCODING, 'x-gzip')){

                        $ENCODING = "x-gzip";
                        $Error_Msg = str_replace('<br>','',GetHeader()
);
                        $Error_Msg .= "Time: ".date("Y-m-d H:i:s")."
";
                        $Error_Msg .= "Remote-Address: ".$REMOTE_ADDR.
" ";
                        //mail('your@none.net', "User have x-gzip outp
ut in file $PHP_SELF!!!", $Error_Msg);
                }else{
                        $ENCODING = "gzip";
                }
                return $ENCODING;
        }else{
                return 0;
        }
}
function GzDocOut(){
        global $PHP_SELF, $CatocGz, $REMOTE_ADDR, $S_UserName;
        $ENCODING = CheckCanGzip();
        if ($ENCODING){
                print " <!-- Use compress $ENCODING --> ";
                $Contents = ob_get_contents();
                ob_end_clean();
                if ($CatocGz == 'Y'){
                        print "Not compress lenth: ".strlen($Contents)
."<BR>";
                        print "Compressed lenth: ".strlen(gzcompress($
Contents))."<BR>";
                        exit;
                }else{
                        header("Content-Encoding: $ENCODING");
                }
                print pack('cccccccc',0x1f,0x8b,0x08,0x00,0x00,0x00,0x
00,0x00);
                $Size = strlen($Contents);
                $Crc = crc32($Contents);
                $Contents = gzcompress($Contents);
                $Contents = substr($Contents, 0, strlen($Contents) - 4
);
                print $Contents;
                print pack('V',$Crc);
                print pack('V',$Size);
                exit;
        }else{
                ob_end_flush();
                $Error_Msg = str_replace('<br>','',GetHeader());
                $Error_Msg .= "Time: ".date("Y-m-d H:i:s")." ";
                $Error_Msg .= "Remote-Address: ".$REMOTE_ADDR." ";
                //mail('your@none.net', "User can not use gzip output  
in file $PHP_SELF!!!", $Error_Msg);
                exit;
        }
}
?>

PHP 相关文章推荐
在PHP3中实现SESSION的功能(一)
Oct 09 PHP
用Zend Encode编写开发PHP程序
Feb 21 PHP
PHP依赖倒置(Dependency Injection)代码实例
Oct 11 PHP
如何在HTML 中嵌入 PHP 代码
May 13 PHP
php数字运算验证码的实现代码
Jul 30 PHP
WordPress中Gravatar头像缓存到本地及相关优化的技巧
Dec 19 PHP
PHP封装函数实现生成随机的字符串验证码
Jan 24 PHP
PHP重定向与伪静态区别
Feb 19 PHP
PHP实现图片的等比缩放和Logo水印功能示例
May 04 PHP
PHP实现可精确验证身份证号码的工具类示例
May 31 PHP
PHP赋值的内部是如何跑的详解
Jan 13 PHP
php 多个变量指向同一个引用($b = &amp;$a)用法分析
Nov 13 PHP
与数据库连接
Oct 09 #PHP
PHP新手上路(八)
Oct 09 #PHP
网络资源
Oct 09 #PHP
PHP中的正规表达式(一)
Oct 09 #PHP
PHP中的正规表达式(二)
Oct 09 #PHP
smtp邮件发送一例
Oct 09 #PHP
图片存储与浏览一例(Linux+Apache+PHP+MySQL)
Oct 09 #PHP
You might like
PHP 中英文混合排版中处理字符串常用的函数
2007/04/12 PHP
win7下memCache的安装过程(具体操作步骤)
2013/06/28 PHP
PHP获得数组交集与差集的方法
2015/06/10 PHP
PHP函数func_num_args用法实例分析
2015/12/07 PHP
JavaScript实现表格排序方法
2013/06/14 Javascript
关于javascript模块加载技术的一些思考
2014/11/28 Javascript
nodejs 整合kindEditor实现图片上传
2015/02/03 NodeJs
jquery实现翻动fadeIn显示的方法
2015/03/05 Javascript
jquery实现浮动在网页右下角的彩票开奖公告窗口代码
2015/09/04 Javascript
JS表格组件神器bootstrap table详解(强化版)
2016/05/26 Javascript
NodeJs的优势和适合开发的程序
2016/08/14 NodeJs
javascript操作cookie
2017/01/17 Javascript
jQuery+Ajax请求本地数据加载商品列表页并跳转详情页的实现方法
2017/07/12 jQuery
vue轻量级框架无法获取到vue对象解决方法
2019/05/12 Javascript
JavaScript隐式类型转换代码实例
2020/05/29 Javascript
[03:31]DOTA2英雄基础教程 大地之灵
2013/12/17 DOTA
python调用新浪微博API项目实践
2014/07/28 Python
Python字符串和文件操作常用函数分析
2015/04/08 Python
python通过get,post方式发送http请求和接收http响应的方法
2015/05/26 Python
python判断字符串编码的简单实现方法(使用chardet)
2016/07/01 Python
python装饰器深入学习
2018/04/06 Python
Python 中的Selenium异常处理实例代码
2018/05/03 Python
pytorch获取vgg16-feature层输出的例子
2019/08/20 Python
使用Tensorflow将自己的数据分割成batch训练实例
2020/01/20 Python
Python基于数列实现购物车程序过程详解
2020/06/09 Python
HTML5如何实现元素拖拽
2016/03/11 HTML / CSS
伦敦高级内衣品牌:Agent Provocateur(大内密探)
2016/08/23 全球购物
德国奢侈品网上商城:Mytheresa
2016/08/24 全球购物
JD Sports意大利:英国篮球和运动时尚的领导者
2017/10/29 全球购物
运动会致辞稿50字
2014/02/04 职场文书
高校教师自荐信范文
2014/03/13 职场文书
函授毕业生自我鉴定范文
2014/03/25 职场文书
活动总结怎么写
2014/04/28 职场文书
学前班学生评语
2014/12/29 职场文书
同事去世追悼词
2015/06/23 职场文书
Python实现归一化算法详情
2022/03/18 Python