使用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 相关文章推荐
我的论坛源代码(六)
Oct 09 PHP
PHP 开发工具
Dec 06 PHP
PHP中删除变量时unset()和null的区别分析
Jan 27 PHP
关于crontab的使用详解
Jun 24 PHP
PHP base64编码后解码乱码的解决办法
Jun 19 PHP
自编函数解决pathinfo()函数处理中文问题
Nov 03 PHP
PHP7.1新功能之Nullable Type用法分析
Sep 26 PHP
PHP对称加密算法(DES/AES)类的实现代码
Nov 14 PHP
PHP中递归的实现实例详解
Nov 14 PHP
Swoole 5将移除自动添加Event::wait()特性详解
Jul 10 PHP
laravel框架中控制器的创建和使用方法分析
Nov 23 PHP
PHP字符串和十六进制如何实现互相转换
Jul 16 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
支持php4、php5的mysql数据库操作类
2008/01/10 PHP
关于Appserv无法打开localhost问题的解决方法
2009/10/16 PHP
通过具体程序来理解PHP里面的抽象类
2010/01/28 PHP
php学习之数据类型之间的转换代码
2011/05/29 PHP
百度地图API应用之获取用户的具体位置
2014/06/10 PHP
php简单判断两个字符串是否相等的方法
2015/07/13 PHP
JavaScript Event学习第三章 早期的事件处理程序
2010/02/07 Javascript
JQuery弹出炫丽对话框的同时让背景变灰色
2014/05/22 Javascript
jquery实现根据浏览器窗口大小自动缩放图片的方法
2015/07/17 Javascript
jQuery实现右侧显示可向左滑动展示的深色QQ客服效果代码
2015/10/23 Javascript
jQuery遍历DOM节点操作之filter()方法详解
2016/04/14 Javascript
hovertree插件实现二级树形菜单(简单实用)
2016/12/28 Javascript
jQuery使用正则表达式替换dom元素标签用法示例
2017/01/16 Javascript
浅谈在Vue-cli里基于axios封装复用请求
2017/11/06 Javascript
design vue 表格开启列排序的操作
2020/10/28 Javascript
Vue实现圆环进度条的示例
2021/02/06 Vue.js
[00:52]黑暗之门更新 新英雄孽主驾临DOTA2
2016/08/24 DOTA
[46:44]VG vs TNC Supermajor小组赛B组败者组决赛 BO3 第一场 6.2
2018/06/03 DOTA
Python实现从URL地址提取文件名的方法
2015/05/15 Python
Python读取视频的两种方法(imageio和cv2)
2018/04/15 Python
python3 pandas 读取MySQL数据和插入的实例
2018/04/20 Python
Python实现合并同一个文件夹下所有txt文件的方法示例
2018/04/26 Python
Django框架的中的setting.py文件说明详解
2018/10/15 Python
python的range和linspace使用详解
2019/11/27 Python
keras之权重初始化方式
2020/05/21 Python
Python HTMLTestRunner测试报告view按钮失效解决方案
2020/05/25 Python
Python3内置函数chr和ord实现进制转换
2020/06/05 Python
Django如何重置migration的几种情景
2021/02/24 Python
八年级英语教学反思
2014/01/09 职场文书
安全生产月演讲稿
2014/05/09 职场文书
县委常委班子对照检查材料思想汇报
2014/09/28 职场文书
婚前协议书范本
2014/10/27 职场文书
鲁滨孙漂流记读书笔记
2015/06/30 职场文书
MySQL连接查询你真的学会了吗?
2021/06/02 MySQL
使用Oracle命令进行数据库备份与还原
2021/12/06 Oracle
JS setTimeout与setInterval的区别
2022/04/20 Javascript