Second try! In my mind, the following source code is right with 4.1.2 and higher, and seems to be right with other versions... and its works with all browser I tried(NS 4.7x, mozilla 1.00rc3, IE5.5, opera6.01)...
As it is said in documentation of the function gzencode, you have to use this function, if you want to get the right result! gzcompress doesn't create the right stream output for browsers!(It is explained in gzencode documentation: generated headers are not the same!)
The source code:
<?
function checkCanGzip() {
global $HTTP_ACCEPT_ENCODING;
if (headers_sent()) return 0;
if (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false) return
"x-gzip";
if (strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false) return
"gzip";
return 0;
}
function gzDocOut() {
if ($encoding = checkCanGzip()) {
$contents = ob_get_contents();
ob_end_clean();
header("Content-Encoding: ".$encoding);
$contents = gzencode($contents);
print($contents);
exit();
}
else {
ob_end_flush();
exit();
}
}
ob_start();
ob_implicit_flush(0);
print("your stuff...");
gzDocOut();
?>
You have to note, that gzencode is able to generate deflate compression, but the previous source code, does not implemente it. You just have to read gzencode documentation to find this tip.
function gzencode in php 4.2.0 (and higher) allows to use a compression level, it is not possible in loder versions.
The code which is commented, is wrong source code, used, in the different exemple found on this page!