I've just spent 5 hours fighting a bug in my application and outcome is:
<?php
// do not use
ob_start("ob_gzhandler");
// along with
header('HTTP/1.1 304 Not Modified');
// or in the end use
ob_end_clean();
?>
W3C Standart requires response body to be empty if 304 header set. With compression on it will at least contain a gzip stream header even if your output is completely empty!
This affects firefox (current ver.3.6.3) in a very subtle way: one of the requests after the one that gets 304 with not empty body gets it response prepended with contents of that body. In my case it was a css file and styles was not rendered at all, which made problem show up.ob_gzhandler
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
ob_gzhandler
Референца за `function.ob-gzhandler.php` со подобрена типографија и навигација.
ob_gzhandler
(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)
ob_gzhandler — ob_start callback функција за gzip бафер
= NULL
ob_gzhandler() е наменета да се користи како callback функција за ob_start() за да помогне во испраќањето на gz-кодирани податоци до веб прелистувачи кои поддржуваат компресирани веб страници. Пред ob_gzhandler() всушност испраќа компресирани податоци, таа утврдува каков тип на кодирање на содржината прелистувачот ќе прифати ("gzip", "deflate" или воопшто ништо) и ќе го врати својот излез соодветно. Сите прелистувачи се поддржани бидејќи зависи од прелистувачот да испрати правилен хеддер со кој се вели дека прифаќа компресирани веб страници. Ако прелистувачот не поддржува компресирани страници, оваа функција враќа false.
Параметри
data-
flags-
Вратени вредности
Примери
Пример #1 ob_gzhandler() example
<?php
ob_start("ob_gzhandler");
?>
<html>
<body>
<p>This should be a compressed page.</p>
</body>
</html>Белешки
Забелешка:
ob_gzhandler() бара zlib extension.
Забелешка:
Не можете да користите и двете ob_gzhandler() and zlib.output_compression. Исто така, имајте предвид дека користењето на zlib.output_compression е претпочитано пред ob_gzhandler().
Види Исто така
- ob_start() - Вклучи баферирање на излезот
- ob_end_flush() Испразни (испрати) ја вратената вредност на активниот ракувач со излез и исклучи го активниот излезен бафер
Белешки од корисници 5 белешки
The simplest way for gzip compression is:
<?php
if(!ob_start("ob_gzhandler")) ob_start();
?>
ob_start("ob_gzhandler") returns FALSE if browser doesn't support gzip, so then is called normal ob_start();About the previous note from Davey:
ob_start(array('ob_gzhandler',9));
Does not work. The output size isn?t affected at all, stays the same.
ob_gzhandler compression level use zlib.output_compression_level, which is -1 per default, level 6.
To change the compression level on the fly, simply use ini_set:
<?php
ini_set('zlib.output_compression_level', 1);
ob_start('ob_gzhandler');
echo 'This is compressed at level 1';
?>In the set_error_handler notes, there is a technique described for capturing all errors (even parse errors) before they are displayed the user, using a special error handler and output handler. If this output handler detects a fatal error in the output buffer, it's captured and dealt with before it can be displayed to the user. If no error was detected, then output buffer is displayed verbatim (i.e. without being compressed).
If you are using this method, you can still take advantage of ob_gzhandler's compression. However, you MUST specify a mode argument (I'm using 4.2.2 on RedHat9). The mode value affects which headers are automatically added (Content-Encoding, etc). A value of '5' worked for me. '0' or discarding the argument produces a blank screen under Mozilla.
<?php
function my_output_handler(&$buffer) {
// Detect errors in the output
if(ereg("(error</b>:)(.+) in <b>(.+)</b> on line <b>(.+)</b>", $buffer, $regs)) {
my_error_handler(E_ERROR, $regs[2], $regs[3], $regs[4]);
// ...
// ... Insert your error handling here ...
// ...
return 'An internal error occurred.';
} else {
// The page rendered without any errors, so compress
// and output.
return ob_gzhandler($buffer, 5);
}
}
?>if you call ob_end_clean() after ob_start("ob_gzhandler"), the "Content-Encoding: gzip" header will still get sent (assuming the browser supports the encoding). if you don't call ob_start() again with the ob_gzhandler callback function, the output will not be compressed, but the header will say it is. this causes mozilla (as of build 2002032808) to display a blank page.