The Ob_* series function is the output buffer that operates on PHP itself.
So, Ob_flush is the buffer that refreshes PHP itself.
And flush, strictly speaking, this only in PHP as the Apache module (handler or filter) installed when the actual role.
It is a buffer that refreshes the webserver (which can be considered specifically Apache).
Under the SAPI of Apache module, flush will invoke Sapi_module's flush member function pointer.
An indirect call to Apache's Api:ap_rflush refreshes Apache's output buffer, and of course the manual says that there are some other Apache modules,
may change the result of this action.
Some Apache modules, such as Mod_gzip, may have their own output caching, which will result in the results of the flush () function not being immediately sent to the client browser.
Even browsers will cache what they receive before it is displayed. For example, 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 will only start to appear after the 256 bytes received, so you must send some extra spaces to allow the browser to display the page content.
So, the order in which the two are used correctly is. First Ob_flush, then flush,
Of course, in other sapi, do not call the flush can also, just to ensure that your code portability, recommended supporting 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 =;
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)};
? >
If you want to support the need to add a response header 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 crucial if you ' re using PHP-FPM. The header is also far more convenient to does on a 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 =;
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)};