使用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 相关文章推荐
建立动态的WML站点(二)
Oct 09 PHP
在PHP里得到前天和昨天的日期的代码
Aug 16 PHP
PHP求最大子序列和的算法实现
Jun 24 PHP
解析PHP处理换行符的问题 \r\n
Jun 13 PHP
php pki加密技术(openssl)详解
Jul 01 PHP
使用pthreads实现真正的PHP多线程(需PHP5.3以上版本)
May 05 PHP
浅谈php自定义错误日志
Feb 13 PHP
php实现求相对时间函数
Jun 15 PHP
thinkphp框架实现数据添加和显示功能
Jun 29 PHP
PHP实现四种基础排序算法的运行时间比较(推荐)
Aug 11 PHP
php使用PDO执行SQL语句的方法分析
Feb 16 PHP
php设计模式之策略模式实例分析【星际争霸游戏案例】
Mar 26 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开发框架总结收藏
2008/04/24 PHP
php 文章采集正则代码
2009/12/28 PHP
php的字符串用法小结
2010/06/08 PHP
PHP编程中的常见漏洞和代码实例
2014/08/06 PHP
使用Huagepage和PGO来提升PHP7的执行性能
2015/11/30 PHP
Yii2框架自定义类统一处理url操作示例
2019/05/25 PHP
laravel 模型查询按照whereIn排序的示例
2019/10/16 PHP
JavaScript实现简单的二级导航菜单实例
2015/04/15 Javascript
JS检测移动端横竖屏的代码
2016/05/30 Javascript
谈谈JS中常遇到的浏览器兼容问题和解决方法
2016/12/17 Javascript
利用JavaScript如何查询某个值是否数组内
2017/07/30 Javascript
使用Jenkins部署React项目的方法步骤
2019/03/11 Javascript
微信小程序把百度地图坐标转换成腾讯地图坐标过程详解
2019/07/10 Javascript
vue-父子组件和ref实例详解
2019/11/10 Javascript
vue 动态设置img的src地址无效,npm run build 后找不到文件的解决
2020/07/26 Javascript
vue 解决无法对未定义的值,空值或基元值设置反应属性报错问题
2020/07/31 Javascript
JS typeof fn === 'function' &amp;&amp; fn()详解
2020/08/22 Javascript
如何实现vue的tree组件
2020/12/03 Vue.js
[01:28:43]2014 DOTA2华西杯精英邀请赛5 24 DK VS CIS
2014/05/25 DOTA
pycharm 使用心得(七)一些实用功能介绍
2014/06/06 Python
python3简单实现微信爬虫
2015/04/09 Python
Python功能键的读取方法
2015/05/28 Python
Python中如何获取类属性的列表
2016/12/26 Python
利用Python在一个文件的头部插入数据的实例
2018/05/02 Python
Flask框架各种常见装饰器示例
2018/07/17 Python
Pytorch实现神经网络的分类方式
2020/01/08 Python
Pycharm中配置远程Docker运行环境的教程图解
2020/06/11 Python
学年末自我鉴定
2014/01/21 职场文书
物流专业自荐信
2014/05/23 职场文书
关于国庆节的演讲稿
2014/09/05 职场文书
外国人来华邀请函
2015/01/31 职场文书
生产现场禁烟通知
2015/04/23 职场文书
医院病假条范文
2015/08/17 职场文书
2016高一新生军训心得体会
2016/01/11 职场文书
利用Python+OpenCV三步去除水印
2021/05/28 Python
Shell脚本一键安装Nginx服务自定义Nginx版本
2022/03/20 Servers