<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>M78星云缓存</title>
	<atom:link href="http://liferar.com/tag/%e7%bc%93%e5%ad%98/feed" rel="self" type="application/rss+xml" />
	<link>http://liferar.com</link>
	<description>愿我们的梦想，像光那样，从一颗星到另一颗星!</description>
	<lastBuildDate>Wed, 07 Dec 2011 16:07:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP输出缓存控制</title>
		<link>http://liferar.com/buffer_control_in_php</link>
		<comments>http://liferar.com/buffer_control_in_php#comments</comments>
		<pubDate>Sat, 28 Mar 2009 14:48:37 +0000</pubDate>
		<dc:creator>CK猪</dc:creator>
				<category><![CDATA[CK猪是程序员]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WEB]]></category>
		<category><![CDATA[缓存]]></category>

		<guid isPermaLink="false">http://www.liferar.com/?p=9</guid>
		<description><![CDATA[有时我们需要页面内容完成生成后一次性输出，或者echo之后马上输出页面内存而不缓存，那么我们就会用到PHP的页面输出缓存控制的相关函数。 PHP页面输出缓存控制函数如下： flush &#8212; 刷新输出缓冲 ob_clean &#8212; Clean (erase) the output buffer ob_end_clean &#8212; Clean (erase) the output buffer and turn off output buffering ob_end_flush &#8212; Flush (send) the output buffer and turn off output buffering ob_flush &#8212; Flush (send) the output buffer ob_get_clean &#8212; Get current buffer contents and delete current output buffer ob_get_contents [...]]]></description>
			<content:encoded><![CDATA[<p>有时我们需要页面内容完成生成后一次性输出，或者echo之后马上输出页面内存而不缓存，那么我们就会用到PHP的页面输出缓存控制的相关函数。</p>
<p>PHP页面输出缓存控制函数如下：</p>
<blockquote><p>flush &#8212; 刷新输出缓冲<br />
ob_clean &#8212; Clean (erase) the output buffer<br />
ob_end_clean &#8212; Clean (erase) the output buffer and turn off output buffering<br />
ob_end_flush &#8212; Flush (send) the output buffer and turn off output buffering<br />
ob_flush &#8212; Flush (send) the output buffer<br />
ob_get_clean &#8212; Get current buffer contents and delete current output buffer<br />
ob_get_contents &#8212; Return the contents of the output buffer<br />
ob_get_flush &#8212; Flush the output buffer, return it as a string and turn off output buffering<br />
ob_get_length &#8212; Return the length of the output buffer<br />
ob_get_level &#8212; Return the nesting level of the output buffering mechanism<br />
ob_get_status &#8212; Get status of output buffers<br />
ob_gzhandler &#8212; ob_start callback function to gzip output buffer<br />
ob_implicit_flush &#8212; Turn implicit flush on/off<br />
ob_list_handlers &#8212; List all output handlers in use<br />
ob_start &#8212; Turn on output buffering<br />
output_add_rewrite_var &#8212; Add URL rewriter values<br />
output_reset_rewrite_vars &#8212; Reset URL rewriter values</p></blockquote>
<p>更加详细的用法见PHP用户手册，下面举一个简单的示例:</p>
<pre lang="php" line="1">
$str = 'Hello world';
echo $str;
sleep(10);
</pre>
<p>这段代码会在sleep了10秒后在页面打印 Hello world。在看下面这段代码：</p>
<pre lang="php" line="1">
$str = 'Hello world';
echo $str . str_repeat(' ', 256);
ob_flush();
flush();
sleep(10);
</pre>
<p>这段代码则会马上在屏幕上打印 Hello world。关键就在于第2和第3行调用的两个函数 ob_flush() 和 flush()。这两个函数得一起使用才能保证页面马上输出Hello world。其中str_repeat(&#8216; &#8216;, 256)则是为了解决某些浏览器必须在接收到256个字符后才会显示内容。下面的内容摘自《PHP用户手册》，很好地解释了上面的代码意图。<br />
引用自《PHP用户手册》的内容</p>
<blockquote><p>
flush() 函数不会对服务器或客户端浏览器的缓存模式产生影响。因此，必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。<br />
个别web服务器程序，特别是Win32下的web服务器程序，在发送结果到浏览器之前，仍然会缓存脚本的输出，直到程序结束为止。<br />
有些Apache的模块，比如mod_gzip，可能自己进行输出缓存，这将导致flush()函数产生的结果不会立即被发送到客户端浏览器。<br />
甚至浏览器也会在显示之前，缓存接收到的内容。例如 Netscape 浏览器会在接受到换行或 html 标记的开头之前缓存内容，并且在接受到  标记之前，不会显示出整个表格。<br />
一些版本的 Microsoft Internet Explorer 只有当接受到的256个字节以后才开始显示该页面，所以必须发送一些额外的空格来让这些浏览器显示页面内容。
</p></blockquote>
<p>上面的代码演示了即时输出缓存，一般情况下该部分代码都能正常完成所要的功能，但是也会有例外</p>
<p>1）服务器打开了gzip等压缩功能，导致输出的缓存被压缩后不足256字节，那么在某些版本IE中也会等到凑够了256字节才输出内容（PS:之所以说某些版本的IE是因为我现在用的IE6就没有该问题，WINDOWS XP SP2)</p>
<p>2)如果这段代码之前还有代码，而且前面的代码中多次调用了ob_start()而ob_end_flush()调用的次数比ob_start()少2次，那么这上面的代码也不能正常工作。因为ob_start()的buffer是stackable的。调用多次ob_start()后，ob_flush()只会把buffer输出到上一层的buffer中。例如</p>
<p>调用ob_start()，它的缓存区为 buffer1，再次调用ob_start()，它的缓存区为buffer2。这时调用ob_flush()只会把 buffer2中的内容输出到 buffer1。这时如果调用 ob_end_flush(), 那么buffer2中的内容会被输出到 buffer1并且销毁buffer2。此时再调用ob_flush()就会把buffer1中的内容输出到服务器，然后调用flush()则可以把服务器中的buffer输出到客户端浏览器。</p>
<p>知道了ob_start()和ob_end_flush()的用法后，就可以用下面的代码来实现所有页面内容完全生成后一次性输出所有的缓存。</p>
<pre lang="php" line="1">
ob_start();

//do something to generate $content
echo $content;

ob_end_flush();
flush(); //if script is ending, this can be removed.
</pre>
<p>之所以要这么做是因为服务器会在缓存区满了以后就输出缓存而不是等到所以页面内容生成后再输出<br />
<h3>相关文章</h3>
<ul class="related_posts">
<li><a href="http://liferar.com/mootools_bugs_date_getweek" title="Mootools Bug - Date.getWeek" rel="bookmark inlinks">Mootools Bug &#8211; Date.getWeek</a><span class="count">( 0 )</span></li>
<li><a href="http://liferar.com/behavior_of_assignment_operator_in_php" title="PHP中=赋值操作符对不同数据类型的不同行为" rel="bookmark inlinks">PHP中=赋值操作符对不同数据类型的不同行为</a><span class="count">( 2 )</span></li>
<li><a href="http://liferar.com/return_value_of_new_function" title="javascript中new function()的返回值（并不一定是function的“实例”）" rel="bookmark inlinks">javascript中new function()的返回值（并不一定是function的“实例”）</a><span class="count">( 0 )</span></li>
<li><a href="http://liferar.com/relationship_between_variable_reference_and_memory_in_php" title="PHP中变量、引用和内存的对应关系" rel="bookmark inlinks">PHP中变量、引用和内存的对应关系</a><span class="count">( 0 )</span></li>
<li><a href="http://liferar.com/httpd_php_compile_parameters" title="httpd、php编译参数" rel="bookmark inlinks">httpd、php编译参数</a><span class="count">( 0 )</span></li>
<li><a href="http://liferar.com/web_dev_problem" title="WEB前端开发的一些问题备忘" rel="bookmark inlinks">WEB前端开发的一些问题备忘</a><span class="count">( 0 )</span></li>
<li><a href="http://liferar.com/html_seldom_used_attribute_list" title="HTML不常用标签属性备忘" rel="bookmark inlinks">HTML不常用标签属性备忘</a><span class="count">( 0 )</span></li>
<li><a href="http://liferar.com/alipay_problems_technical_support" title="支付宝集成经验之技术支持问题" rel="bookmark inlinks">支付宝集成经验之技术支持问题</a><span class="count">( 8 )</span></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://liferar.com/buffer_control_in_php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

