update page now

ob_gzhandler

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

ob_gzhandler ob_start 回调函数压缩输出缓冲区

说明

ob_gzhandler(string $data, int $flags): string|false

ob_gzhandler() 目的用作 ob_start() 的回调函数,以便将 gz 编码的数据发送到支持网页压缩的 Web 浏览器。在 ob_gzhandler() 实际发送压缩数据之前,该函数会确定浏览器接受哪种类型的内容编码("gzip"、"deflate" 或都不接受),然后相应的返回输出。支持所有发送正确头消息以表明接受压缩网页的浏览器。如果浏览器不支持压缩页面,则函数返回 false

参数

data

flags

返回值

示例

示例 #1 ob_gzhandler() 示例

<?php

ob_start
("ob_gzhandler");

?>
<html>
<body>
<p>This should be a compressed page.</p>
</body>
</html>

注释

注意:

ob_gzhandler() 需要 zlib 扩展。

注意:

不能同时使用 ob_gzhandler()zlib.output_compression。也要注意使用 zlib.output_compression 要优于 ob_gzhandler()

参见

  • ob_start() - 打开输出控制缓冲
  • ob_end_flush() - 冲刷(发送)活动输出处理程序的返回值,并关闭活动输出缓冲区

添加备注

用户贡献的备注 5 notes

up
10
Jer
15 years ago
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.
up
6
przemekryciuk at gmail dot com
16 years ago
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();
up
2
daijoubu at videotron dot ca
22 years ago
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';
?>
up
1
jazfresh at SPAM-JAVELIN dot hotmail dot com
22 years ago
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);
  }
}
?>
up
1
xn at bnw dot com
23 years ago
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.
To Top