note 35969 added to function.ob-gzhandler

From: Date: Sun, 21 Sep 2003 06:04:10 +0000
Subject: note 35969 added to function.ob-gzhandler
Groups: php.notes 
Request: Send a blank email to php-notes+get-56838@lists.php.net to get a copy of this message
i had a LOT of trouble trying to get gzipping working correctly i kept getting a blank page on the first load, although subsequent pages loaded fine. i tried stuff with ob_flush and ob_end_clean and all kinds of stuff i didn't understand. i'm totally confused about different versions of php and different methods of gzipping. in the end, i got it working (maybe i'll understand it later). i ended up using phpBB's code (stripped of non-gz stuff, and ever-so-slightly modified) http://phpbb.com/ index.php: ------------ <?php require_once('top.php'); echo "<html>\n<head>\n<title>Gzip Test</title>\n<body>\n<h1>testing</h1>\n</body>\n</html>"; require_once('bottom.php'); ?> gz_header.php - taken form phpBB's page_header.php -------------- <?php $phpver = phpversion(); $useragent = (isset($_SERVER["HTTP_USER_AGENT"]) ) ? $_SERVER["HTTP_USER_AGENT"] : $HTTP_USER_AGENT; if ( $phpver >= '4.0.4pl1' && ( strstr($useragent,'compatible') || strstr($useragent,'Gecko') ) ) { if ( extension_loaded('zlib') ) { ob_start('ob_gzhandler'); } } else if ( $phpver > '4.0' ) { if ( strstr($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], 'gzip') ) { if ( extension_loaded('zlib') ) { $do_gzip_compress = TRUE; ob_start(); ob_implicit_flush(0); header('Content-Encoding: gzip'); } } } ?> gz_footer.php - taken form phpBB's page_tail.php ------------ <?php // Compress buffered output if required and send to browser if ( $do_gzip_compress ) { // // Borrowed from php.net! // $gzip_contents = ob_get_contents(); ob_end_clean(); $gzip_size = strlen($gzip_contents); $gzip_crc = crc32($gzip_contents); $gzip_contents = gzcompress($gzip_contents, 9); $gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4); echo "\x1f\x8b\x08\x00\x00\x00\x00\x00"; echo $gzip_contents; echo pack('V', $gzip_crc); echo pack('V', $gzip_size); } exit; ?> i must admit to not seeing this working in opera 7.11. perhaps i will figure that one out. if there is anything i should change email me and i can edit my post ---- Manual Page -- http://www.php.net/manual/en/function.ob-gzhandler.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35969 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35969&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35969&report=yes

« previous php.notes (#56838) next »