Mega Code Archive

 
Categories / Php / HTML
 

PHP4 HTTP Compression Speeds up the Web

<? 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; } } ?>