ob_*系列函數, 是操作PHP本身的輸出緩沖區.所以, ob_flush是刷新PHP自身的緩沖區.而flush, 嚴格來講, 這個只有在PHP做為apache的Module(handler或者filter)安裝的時候, 才有實際作用. 它是刷新WebServer(可以認為特指apache)的緩沖區.在apache module的sapi下, flush會通過調用sapi_module的flush成員函數指針, 間接的調用apache的api: ap_rflush刷新apache的輸出緩沖區, 當然手冊中也說了, 有一些apache的其他模塊, 可能會改變這個動作的結果..有些Apache的模塊,比如mod_gzip,可能自己進行輸出緩存,這將導致flush()函數產生的結果不會立即被發送到客戶端瀏覽器。甚至瀏覽器也會在顯示之前,緩存接收到的內容。例如 Netscape瀏覽器會在接受到換行或 html標記的開頭之前緩存內容,并且在接受到 標記之前,不會顯示出整個表格。一些版本的 Microsoft Internet Explorer 只有當接受到的256個字節以后才開始顯示該頁面,所以必須發送一些額外的空格來讓這些瀏覽器顯示頁面內容。所以, 正確使用倆者的順序是. 先ob_flush, 然后flush,當然, 在其他sapi下, 不調用flush也可以, 只不過為了保證你代碼的可移植性, 建議配套使用.// 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)}; 如果要在 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 do on an as-needed basis. Docs on X-Accel-Buffering http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffering ;// 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)}; PHP教程