Ob_flush/flush's description in the manual, is to refresh the output buffer, and also need to be used, so it will cause many people confused ... In fact, the two of them have different objects, and in some cases, flush doesn't do anything at all.
The Ob_* series function is the output buffer that operates PHP itself.
So, Ob_flush is the buffer that refreshes PHP itself.
Flush, strictly speaking, this only works when PHP is installed as an Apache module (handler or filter).
It is a buffer that refreshes the webserver (which can be considered specifically Apache).
Under Apache module SAPI, flush will be called by the Flush member function pointer of Sapi_module.
The indirect call to Apache's Api:ap_rflush refreshes Apache's output buffers, and of course the manual also says that there are some other Apache modules,
may change the result of this action.
Some Apache modules, such as Mod_gzip, may make their own output caches, which will result in the flush () function producing results that are not immediately sent to the client browser.
Even the browser will cache the received content before it is displayed. For example, the Netscape browser caches content before it accepts the start of a newline or HTML tag, and does not display the entire table until the tag is accepted.
Some versions of Microsoft Internet Explorer
The page is only displayed after 256 bytes have been accepted, so you must send some extra spaces to let these browsers display the contents of the page.
So, the correct use of the two is the order. First Ob_flush, then flush,
Of course, in other sapi, do not call flush also can, only to ensure that your code portability, recommended for use.
<?php//set_time_limit (0); header (' Content-type:text/event-stream '); header (' Cache-control:no-cache ');//Ob_end _flush ();//Ini_set (' output_buffering ', 0);//Ini_set (' Implicit_flush ', 1); if (ob_get_level () = = 0) ob_start (); Echo str _repeat ("', 4096); $long = 60;while ($long > 0) {$time = date (' R '); echo" Data:the server time is: {$time}\n\n "; Ob_flush ( ); flush ();//break;sleep (1); $long--;} var source=new EventSource ("http://localhost:18000/sse.php"); Source.onmessage=function (event) {Console.info ( Event.data)};? >
Add a response header if you want to support on Nginx + FPM + PHP
Header (' X-accel-buffering:no ');
This eliminates both proxy_buffering and (if you have Nginx >= 1.5.6), fastcgi_buffering. The FASTCGI bit is a crucial if you ' re using PHP-FPM. The header is also far and convenient to does on an as-needed basis.
Docs on x-accel-buffering http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffering;
<?php//set_time_limit (0); header (' Content-type:text/event-stream '); header (' Cache-control:no-cache '); Header (' X-accel-buffering:no ');//Ob_end_flush ();//Ini_set (' output_buffering ', 0);//Ini_set (' Implicit_flush ', 1);/if (ob_ Get_level () = = 0) ob_start ();//Echo Str_repeat (", 4096); $long = 60;while ($long > 0) {$time = date (' R '); echo" data:th E server time is: {$time}\n\n "; Ob_flush (); flush ();//break;sleep (1); $long--;} var source=new EventSource ("http://localhost:18000/sse.php"); Source.onmessage=function (event) {Console.info ( Event.data)};? >
Summary: The above is the entire content of this article, I hope to be able to help you learn.