flush
(PHP 3, PHP 4, PHP 5)
flush -- 刷新輸出緩沖
說明
void flush ( void )
刷新PHP程序的緩沖,而不論P(yáng)HP執(zhí)行在何種情況下(CGI ,web服務(wù)器等等)。該函數(shù)將當(dāng)前為止程序的所有輸出發(fā)送到用戶的瀏覽器。
flush() 函數(shù)不會對服務(wù)器或客戶端瀏覽器的緩存模式產(chǎn)生影響。因此,必須同時使用 ob_flush() 和flush() 函數(shù)來刷新輸出緩沖。
個別web服務(wù)器程序,特別是Win32下的web服務(wù)器程序,在發(fā)送結(jié)果到瀏覽器之前,仍然會緩存腳本的輸出,直到程序結(jié)束為止。
有些Apache的模塊,比如mod_gzip,可能自己進(jìn)行輸出緩存,這將導(dǎo)致flush()函數(shù)產(chǎn)生的結(jié)果不會立即被發(fā)送到客戶端瀏覽器。
甚至瀏覽器也會在顯示之前,緩存接收到的內(nèi)容。例如 Netscape 瀏覽器會在接受到換行或 html 標(biāo)記的開頭之前緩存內(nèi)容,并且在接受到 </table> 標(biāo)記之前,不會顯示出整個表格。
一些版本的 Microsoft Internet Explorer 只有當(dāng)接受到的256個字節(jié)以后才開始顯示該頁面,所以必須發(fā)送一些額外的空格來讓這些瀏覽器顯示頁面內(nèi)容。
ob_flush
(PHP 4 >= 4.2.0, PHP 5)
ob_flush -- Flush (send) the output buffer
Description
void ob_flush ( void )
This function will send the contents of the output buffer (if any). If you want to further process the buffer's contents you have to call ob_get_contents() before ob_flush() as the buffer contents are discarded after ob_flush() is called.
This function does not destroy the output buffer like ob_end_flush() does.
flush和ob_flush的使用上有一些特別容易犯錯的地方,造成無法刷新輸出緩沖。
一. flush和ob_flush的正確順序,正確應(yīng)是,先ob_flush再flush,如下:
ob_flush();
flush();
如果Web服務(wù)器的操作系統(tǒng)是windows系統(tǒng),那順序顛倒或者不使用ob_flush()也不會出現(xiàn)問題。但是在Linux系統(tǒng)上就無法刷新輸出緩沖。
二. 使用ob_flush()前,確保前面的內(nèi)容大小足夠4069字符。
一些Web服務(wù)器的output_buffering默認(rèn)是4069字符或者更大,即輸出內(nèi)容必須達(dá)到4069字符服務(wù)器才會flush刷新輸出緩沖,為了確保flush有效,最好在ob_flush()函數(shù)前有以下語句:
print str_repeat(" ", 4096);
以確保到達(dá)output_buffering值。
for ($i=10; $i>0; $i--)
{
echo $i.'<br />';
ob_flush();
flush();
sleep(1);
}
ob_end_flush();
新聞熱點(diǎn)
疑難解答
圖片精選