<?
/********************************************************************
** Title.........: PHP4 HTTP Compression Speeds up the Web
** Version.......: 1.11
** Author........: catoc <catoc@163.net>
** Filename......: gzdoc.php
** Last changed..: 14/02/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.
** PHP 4.0.4 supports built-in HTTP compression.
** Version 1.11 cleaning code.
** Usage........:
** No space before the beginning of the first '<?' tag.
** ------------Start of file----------
** |<?
** | include('gzdoc.php');
** |?>
** |<HTML>
** |... the page ...
** |</HTML>
** |<?
** | gzdocout();
** |?>
** -------------End of file-----------
*******************************************************************/
ob_start();
ob_implicit_flush(0);
function GzDocOut(){
global $HTTP_ACCEPT_ENCODING, $DEBUG;
$DEBUG = ($DEBUG) ? $DEBUG : 1;
if(preg_match('/(x-gzip|gzip)/', $HTTP_ACCEPT_ENCODING, $EncodeArr)){
$ENCODING = $EncodeArr[1];
$Contents = ob_get_contents();
ob_end_clean();
header("Content-Encoding: $ENCODING");
$Contents .= ($DEBUG) ? "\n<!-- Use compress $ENCODING -->" : '';
if($DEBUG == 2){
$Contents .= "\n<!-- Not compress length: ";
$Contents .= number_format(strlen($Contents) / 1024, 2) . 'K -->';
$Contents .= "\n<!-- Compressed length: ";
$Contents .= number_format(strlen(gzcompress($Contents)) / 1024, 2) . 'K -->';
}
$Size = strlen($Contents);
$Crc = crc32($Contents);
$Contents = gzcompress($Contents);
print pack('c*',0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00);
print substr($Contents, 0, strlen($Contents) - 4);
print pack('V*',$Crc,$Size);
exit;
}else{
print ($DEBUG) ? '<!-- Not compress -->' : '';
ob_end_flush();
exit;
}
}
?>
but use it carefully, i ran it on a site with about 1000 connections at an avarage.
and the serverload was up to 35 in notime, so i took it back off
cat oc wrote :533
The ob_gzhandler has a huge memory leak in php 4.0.4 && php 4.0.4pl1 and it is already fixed in CVS. IF you do want to use the handler please update using the last CVS version.