note 88044 modified in function.gzcompress by danbrown

From: Date: Thu, 08 Jan 2009 15:42:12 +0000
Subject: note 88044 modified in function.gzcompress by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148953@lists.php.net to get a copy of this message
To properly use gzcompress to send compressed data to browsers that support it (most of modern browsers do, even mobile ones !), use this function (I dont think W3C validator issue mentioned earlier is valid ). use ob_start() at start of your script also <?php function print_gzipped_output() { $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"]; if( headers_sent() ) $encoding = false; else if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ) $encoding = 'x-gzip'; else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ) $encoding = 'gzip'; else $encoding = false; if( $encoding ) { $contents = ob_get_clean(); $_temp1 = strlen($contents); if ($_temp1 < 2048) // no need to waste resources in compressing very little data print($contents); else { header('Content-Encoding: '.$encoding); print("\x1f\x8b\x08\x00\x00\x00\x00\x00"); $contents = gzcompress($contents, 9); $contents = substr($contents, 0, $_temp1); print($contents); } } else ob_end_flush(); } ?> --was-- To properly use gzcompress to send compressed data to browsers that support it (most of modern browsers do, even mobile ones !), use this function (I dont think W3C validator issue mentioned earlier is valid ). use ob_start() at start of your script also function print_gzipped_output() { $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"]; if( headers_sent() ) $encoding = false; else if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ) $encoding = 'x-gzip'; else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ) $encoding = 'gzip'; else $encoding = false; if( $encoding ) { $contents = ob_get_clean(); $_temp1 = strlen($contents); if ($_temp1 < 2048) // no need to waste resources in compressing very little data print($contents); else { header('Content-Encoding: '.$encoding); print("\x1f\x8b\x08\x00\x00\x00\x00\x00"); $contents = gzcompress($contents, 9); $contents = substr($contents, 0, $_temp1); print($contents); } } else ob_end_flush(); } http://php.net/manual/en/function.gzcompress.php

« previous php.notes (#148953) next »