ob_start
(PHP 4, PHP 5, PHP 7)
ob_start — 打开输出控制缓冲
此函数将打开输出缓冲。当输出缓冲激活后,脚本将不会输出内容(除http标头外),相反需要输出的内容被存储在内部缓冲区中。
内部缓冲区的内容可以用 ob_get_contents() 函数复制到一个字符串变量中。 想要输出存储在内部缓冲区中的内容,可以使用 ob_end_flush() 函数。另外, 使用 ob_end_clean() 函数会静默丢弃掉缓冲区的内容。
当有正在调用的回调函数时,一些网络服务器(例如Apache)会改变一个脚本的工作目录。 你可以在回调函数中再把它改回来,例如 chdir(dirname($_SERVER['SCRIPT_FILENAME'])) 。
输出缓冲区是可堆叠的,这即意谓着,当有一个 ob_start() 是活跃的时, 你可以调用另一个 ob_start() 。 只要确保又正确调用了 ob_end_flush() 恰当的次数即可。 如果有多重输出回调函数是活跃的,输出内容会一直按嵌套的顺序依次通过它们而被过滤。
ob_flush
(PHP 4 >= 4.2.0, PHP 5, PHP 7)
ob_flush — 冲刷出(送出)输出缓冲区中的内容
这个函数将送出缓冲区的内容(如果里边有内容的话)。如果想进一步处理缓冲区中的内容,必须在ob_flush()之前调用ob_get_contents() ,因为在调用ob_flush()之后缓冲区内容将被丢弃。
此函数不会销毁输出缓冲区,而像ob_end_flush() 函数会销毁缓冲区。
flush
(PHP 4, PHP 5, PHP 7)
flush — 刷新输出缓冲
刷新PHP程序的缓冲,而不论PHP执行在何种情况下(CGI ,web服务器等等)。该函数将当前为止程序的所有输出发送到用户的浏览器。
flush() 函数不会对服务器或客户端浏览器的缓存模式产生影响。因此,必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
个别web服务器程序,特别是Win32下的web服务器程序,在发送结果到浏览器之前,仍然会缓存脚本的输出,直到程序结束为止。
有些Apache的模块,比如mod_gzip,可能自己进行输出缓存,这将导致flush()函数产生的结果不会立即被发送到客户端浏览器。
甚至浏览器也会在显示之前,缓存接收到的内容。例如 Netscape 浏览器会在接受到换行或 html 标记的开头之前缓存内容,并且在接受到
标记之前,不会显示出整个表格。
一些版本的 Microsoft Internet Explorer 只有当接受到的256个字节以后才开始显示该页面,所以必须发送一些额外的空格来让这些浏览器显示页面内容。
Nginx + php-fpm
检查nginx配置文件(nginx.conf),禁用nginx的buffering,如果有gzip配置,建议注释掉:
#gzip_min_length 1000;
#gzip_buffers 4 16k;
#gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
proxy_buffering off;
gzip off;
fastcgi_keep_conn on;
检查php.ini,禁用buffering:
; On = Enabled and buffer is unlimited. (Use with caution)
; Off = Disabled
; Integer = Enables the buffer and sets its maximum size in bytes.
; Note: This directive is hardcoded to Off for the CLI SAPI
; Default Value: Off
; Development Value: 4096
; Production Value: 4096
; http://php.net/output-buffering
; output_buffering = 4096
output_buffering = off
php此项配置,不能使用ini_set方法:
the output_buffering setting is PHP_INI_PERDIR therefore it may not be set using ini_set()
PHP buffer ob_flush() vs. flush()
参考:
https://stackoverflow.com/questions/4191385/php-buffer-ob-flush-vs-flush
ob_flush sends an application-initiated buffer. There may be multiple nested ob_start()'s in any PHP script. ob_flush passes the current content to the upper layer.
PHP itself might (at its own discretion) buffer output. This depends on the back-end. But usually FastCGI has a socket buffer on its own. Therefore flush() needs to be invoked as well to send the current content to the web server.
And now the web server might itself implement another buffering scheme (mod_deflate or content filter), which you have no influence over. But this is seldom, as it needs to be configured specifically.
大概意思就是ob_flush发送应用程序启动的缓冲区,我们编写的php代码中可以有多个嵌套的ob_start,ob_flush会将当前内容传递给上层嵌套。而由于php外层还会包含后端服务比如FastCGII,而FastCGII自身有一个cocket buffer,我们需要调用flush方法才能将内容传输给web服务器,最终web服务器传给浏览器。
flush和ob_flush的正确顺序,先ob_flush,再flush。
ob_end_flush与ob_end_clean
这二个函数有点相似,都会关闭ouptu_buffering机制。但不同的是,ob_end_flush只是把php buffer中的数据冲(flush/send)到客户端浏览器,而ob_clean_clean将php bufeer中的数据清空(erase),但不发送给客户端浏览器。
ob_end_flush调用之前 ,php buffer中的数据依然存在,ob_get_contents()依然可以获取php buffer中的数据拷贝。
而ob_end_flush()调用之后 ob_get_contents()取到的是空字符串,同时浏览器也接收不到输出,即没有任何输出。
可以使用ob_get_contents()以字符串形式获取服务端缓存的数据,使用ob_end_flush()则会输出被缓存起来的数据,并关闭缓存。
而使用ob_end_clean()则会静默的清除服务端缓存的数据,而不会有任何数据或其他行为。
服务端的缓存是堆叠起来的,也就是说你在开启了ob_start()后,关闭之前,在其内部还可以开启另外一个缓存ob_start()。不过你也要务必保证关闭缓存的操作和开启缓存的操作数量一样多。
ob_start() 可以指定一个回调函数来处理缓存数据,如果一个ob_start()内部嵌套了另一个ob_start(),我们假定,外层的ob_start(),编号是A,内层的ob_start()编号是B,它们各自制定了一个回调函数分别是functionA和functionB,那么在缓存B中的数据输出时,它会先辈funcitonB回调函数处理,再交给外层的functionA回调函数处理,之后才能输出到客户端。
代码示例
以下php代码,浏览器访问时会每隔1秒输出1个数字,共输出3个。
ob_start();
for($i = 1; $i <= 3; $i++)
{
echo "$i<br/>";
ob_flush();
flush();
sleep(1);
}
ob_end_flush();
Leave a Reply