Precautions for using the PHP flush function

Source: Internet
Author: User
Tags sapi
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: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)};? >

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.