使用 php4 加速 web 传输 |
| 时间:2007-07-13 20:03:12 来源:Linux联盟收集整理 作者: |
<? tDPLinux联盟 /*************************************** tDPLinux联盟 ** Title.........: PHP4 HTTP Compression Speeds up the Web tDPLinux联盟 ** Version.......: 1.10 tDPLinux联盟 ** Author........: catoc <catoc@163.net> tDPLinux联盟 ** Filename......: gzdoc.php tDPLinux联盟 ** Last changed..: 25/08/2000 tDPLinux联盟 ** Requirments...: PHP4 >= 4.0.1 tDPLinux联盟 ** PHP was configured with --with-zlib[=DIR] tDPLinux联盟 ** Notes.........: Dynamic Content Acceleration compresses tDPLinux联盟 ** the data transmission data on the fly tDPLinux联盟 ** code by sun jin hu (catoc) <catoc@163.net> tDPLinux联盟 ** Most newer browsers since 1998/1999 have tDPLinux联盟 ** been equipped to support the HTTP 1.1 tDPLinux联盟 ** standard known as "content-encoding." tDPLinux联盟 ** Essentially the browser indicates to the tDPLinux联盟 ** server that it can accept "content encoding" tDPLinux联盟 ** and if the server is capable it will then tDPLinux联盟 ** compress the data and transmit it. The tDPLinux联盟 ** browser decompresses it and then renders tDPLinux联盟 ** the page. tDPLinux联盟 ** Useage........: tDPLinux联盟 ** No space before the beginning of the first '<?' tag. tDPLinux联盟 ** ------------Start of file---------- tDPLinux联盟 ** |<? tDPLinux联盟 ** | include('gzdoc.php'); tDPLinux联盟 ** | print "Start output !!"; tDPLinux联盟 ** |?> tDPLinux联盟 ** |<HTML> tDPLinux联盟 ** |... the page ... tDPLinux联盟 ** |</HTML> tDPLinux联盟 ** |<? tDPLinux联盟 ** | gzdocout(); tDPLinux联盟 ** |?> tDPLinux联盟 ** -------------End of file----------- tDPLinux联盟 ***************************************/ tDPLinux联盟 ob_start(); tDPLinux联盟 ob_implicit_flush(0); tDPLinux联盟 function GetHeader(){ tDPLinux联盟 $headers = getallheaders(); tDPLinux联盟 while (list($header, $value) = each($headers)) { tDPLinux联盟 $Message .= "$header: $value<br>\n"; tDPLinux联盟 } tDPLinux联盟 return $Message; tDPLinux联盟 } tDPLinux联盟 function CheckCanGzip(){ tDPLinux联盟 global $HTTP_ACCEPT_ENCODING, $PHP_SELF, $Wget, $REMOTE_ADDR, $S_UserName; tDPLinux联盟 if (connection_timeout() || connection_aborted()){ tDPLinux联盟 return 0; tDPLinux联盟 } tDPLinux联盟 if ((strpos('catoc'.$HTTP_ACCEPT_ENCODING, 'gzip')) || $Wget == 'Y'){ tDPLinux联盟 if (strpos('catoc'.$HTTP_ACCEPT_ENCODING, 'x-gzip')){ tDPLinux联盟 $ENCODING = "x-gzip"; tDPLinux联盟 $Error_Msg = str_replace('<br>','',GetHeader()); tDPLinux联盟 $Error_Msg .= "Time: ".date("Y-m-d H:i:s")."\n"; tDPLinux联盟 $Error_Msg .= "Remote-Address: ".$REMOTE_ADDR."\n"; tDPLinux联盟 //mail('your@none.net', "User have x-gzip output in file $PHP_SELF!!!", $Error_Msg); tDPLinux联盟 }else{ tDPLinux联盟 $ENCODING = "gzip"; tDPLinux联盟 } tDPLinux联盟 return $ENCODING; tDPLinux联盟 }else{ tDPLinux联盟 return 0; tDPLinux联盟 } tDPLinux联盟 } tDPLinux联盟 function GzDocOut(){ tDPLinux联盟 global $PHP_SELF, $CatocGz, $REMOTE_ADDR, $S_UserName; tDPLinux联盟 $ENCODING = CheckCanGzip(); tDPLinux联盟 if ($ENCODING){ tDPLinux联盟 print "\n<!-- Use compress $ENCODING -->\n"; tDPLinux联盟 $Contents = ob_get_contents(); tDPLinux联盟 ob_end_clean(); tDPLinux联盟 if ($CatocGz == 'Y'){ tDPLinux联盟 print "Not compress lenth: ".strlen($Contents)."<BR>"; tDPLinux联盟 print "Compressed lenth: ".strlen(gzcompress($Contents))."<BR>"; tDPLinux联盟 exit; tDPLinux联盟 }else{ tDPLinux联盟 header("Content-Encoding: $ENCODING"); tDPLinux联盟 } tDPLinux联盟 print pack('cccccccc',0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00); tDPLinux联盟 $Size = strlen($Contents); tDPLinux联盟 $Crc = crc32($Contents); tDPLinux联盟 $Contents = gzcompress($Contents); tDPLinux联盟 $Contents = substr($Contents, 0, strlen($Contents) - 4); tDPLinux联盟 print $Contents; tDPLinux联盟 print pack('V',$Crc); tDPLinux联盟 print pack('V',$Size); tDPLinux联盟 exit; tDPLinux联盟 }else{ tDPLinux联盟 ob_end_flush(); tDPLinux联盟 $Error_Msg = str_replace('<br>','',GetHeader()); tDPLinux联盟 $Error_Msg .= "Time: ".date("Y-m-d H:i:s")."\n"; tDPLinux联盟 $Error_Msg .= "Remote-Address: ".$REMOTE_ADDR."\n"; tDPLinux联盟 //mail('your@none.net', "User can not use gzip output in file $PHP_SELF!!!", $Error_Msg); tDPLinux联盟 exit; tDPLinux联盟 } tDPLinux联盟 } tDPLinux联盟 ?>tDPLinux联盟
|
|
|
|
|
|