【转载】原文地址:http://www.itbbs.cn/index.php?showtopic=1074
Output Control 函数可以让你自由控制脚本中数据的输出。它非常地有用,特别是对于:当你想在数据已经输出后,再输出文件头的情况。输出控制函数不对使用 header() 或 setcookie(), 发送的文件头信息产生影响,只对那些类似于 echo() 和 PHP 代码的数据块有作用。
我们先举一个简单的例子,让大家对Output Control有一个大致的印象:
Example 1.
<span><br> <span><?php <BR>ob_start</span><span>(); </span><span>//打开缓冲区 <br></span><span>echo </span><span>"Hellon\"; //输出 <br>header("</span><span>location</span><span>:</span><span>index</span><span>.</span><span>php</span><span>"); //把浏览器重定向到index.php <br>ob_end_flush();//输出全部内容到浏览器 <br>?> <br> </span></span>
<span><br><span><?php <BR></span><span>for(</span><span>$i </span><span>= </span><span>1</span><span>; </span><span>$i </span><span><span>300</span><span>; </span><span>$i</span><span>++ ) print(</span><span>" "</span><span>); <br></span><span>// 这一句话非常关键,cache的结构使得它的内容只有达到一定的大小才能从浏览器里输出 <br>// 换言之,如果cache的内容不达到一定的大小,它是不会在程序执行完毕前输出的。经 <br>// 过测试,我发现这个大小的底限是256个字符长。这意味着cache以后接收的内容都会 <br>// 源源不断的被发送出去。 <br></span><span>For(</span><span>$j </span><span>= </span><span>1</span><span>; </span><span>$j </span><span><span>20</span><span>; </span><span>$j</span><span>++) { <br>echo </span><span>$j</span><span>.</span><span>" <br>"</span><span>; <br></span><span>flush</span><span>(); </span><span>//这一部会使cache新增的内容被挤出去,显示到浏览器上 <br></span><span>sleep</span><span>(</span><span>1</span><span>); </span><span>//让程序"睡"一秒钟,会让你把效果看得更清楚 <br></span><span>} <br></span><span>?></span> <br></span></span></span>
<span><br><span><?php <BR>ob_start</span><span>(); </span><span>//打开缓冲区 <br></span><span>phpinfo</span><span>(); </span><span>//使用phpinfo函数 <br></span><span>$info</span><span>=</span><span>ob_get_contents</span><span>(); </span><span>//得到缓冲区的内容并且赋值给$info <br></span><span>$file</span><span>=</span><span>fopen</span><span>('</span><span>info.txt</span><span>\'</span><span>,</span><span>\'</span><span>w</span><span>\'</span><span>); //打开文件info.txt <br>fwrite($file,$info); //写入信息到info.txt <br>fclose($file); //关闭文件info.txt <br>?> <br></span></span>
<span><br><span><?php <BR>ob_start</span><span>();</span><span>//打开缓冲区 <br></span><span>?></span> <br>php页面的全部输出 <br><span> <br>$content </span><span>= </span><span>ob_get_contents</span><span>();</span><span>//取得php页面输出的全部内容 <br></span><span>$fp </span><span>= </span><span>fopen</span><span>(</span><span>"output00001.html"</span><span>, </span><span>"w"</span><span>); </span><span>//创建一个文件,并打开,准备写入 <br></span><span>fwrite</span><span>(</span><span>$fp</span><span>, </span><span>$content</span><span>); </span><span>//把php页面的内容全部写入output00001.html,然后…… <br></span><span>fclose</span><span>(</span><span>$fp</span><span>); <br></span><span>?></span> <br></span>
<span><br><span> <br></span><span>Function </span><span>run_code</span><span>(</span><span>$code</span><span>) { <br>If(</span><span>$code</span><span>) { <br></span><span>ob_start</span><span>(); <br>eval(</span><span>$code</span><span>); <br></span><span>$contents </span><span>= </span><span>ob_get_contents</span><span>(); <br></span><span>ob_end_clean</span><span>(); <br>}else { <br>echo </span><span>"错误!没有输出"</span><span>; <br>exit(); <br>} <br>return </span><span>$contents</span><span>; <br>} <br></span></span>
<span><br><span> <br></span><span>/* <br>** Title.........: PHP4 HTTP Compression Speeds up the Web <br>** Version.......: 1.20 <br>** Author........: catoc <br>** Filename......: gzdoc.php <br>** Last changed..: 18/10/2000 <br>** Requirments...: PHP4 >= 4.0.1 <br>** PHP was configured with --with-zlib[=DIR] <br>** Notes.........: Dynamic Content Acceleration compresses <br>** the data transmission data on the fly <br>** code by sun jin hu (catoc) <br>** Most newer browsers since 1998/1999 have <br>** been equipped to support the HTTP 1.1 <br>** standard known as \"content-encoding.\" <br>** Essentially the browser indicates to the <br>** server that it can accept \"content encoding\" <br>** and if the server is capable it will then <br>** compress the data and transmit it. The <br>** browser decompresses it and then renders <br>** the page. <br>** <br>** Modified by John Lim ([email]jlim@natsoft.com.my[/email]) <br>** based on ideas by Sandy McArthur, Jr <br>** Usage........: <br>** No space before the beginning of the first \'\' tag. <br>** ------------Start of file---------- <br>** | <br>** | include(\'gzdoc.php\'); <br>** |? > <br>** | <br>** |... the page ... <br>** | <br>** | <br>** | gzdocout(); <br>** |? > <br>** -------------End of file----------- <br>*/ <br></span><span>ob_start</span><span>(); <br></span><span>ob_implicit_flush</span><span>(</span><span>0</span><span>); <br>function </span><span>CheckCanGzip</span><span>(){ <br>global </span><span>$HTTP_ACCEPT_ENCODING</span><span>; <br>if (</span><span>headers_sent</span><span>() || </span><span>connection_timeout</span><span>() || </span><span>connection_aborted</span><span>()){ <br>return </span><span>0</span><span>; <br>} <br>if (</span><span>strpos</span><span>(</span><span>$HTTP_ACCEPT_ENCODING</span><span>, '</span><span>x-gzip</span><span>\'</span><span>) !== false) return \"x-gzip\"; <br>if (strpos($HTTP_ACCEPT_ENCODING,</span><span>\'</span><span>gzip</span><span>\'</span><span>) !== false) return \"gzip\"; <br>return 0; <br>} <br>/* $level = compression level 0-9, 0=none, 9=max */ <br>function GzDocOut($level=1,$debug=0){ <br>$ENCODING = CheckCanGzip(); <br>if ($ENCODING){ <br>print \"n<!-- Use compress $ENCODING -->n\"; <br>$Contents = ob_get_contents(); <br>ob_end_clean(); <br>if ($debug){ <br>$s = \"<p>Not compress length: \".strlen($Contents); <br>$s .= \" <br>Compressed length: \".strlen(gzcompress($Contents,$level)); <br>$Contents .= $s; <br>} <br>header(\"Content-Encoding: $ENCODING\"); <br>print \"x1fx8bx08x00x00x00x00x00\"; <br>$Size = strlen($Contents); <br>$Crc = crc32($Contents); <br>$Contents = gzcompress($Contents,$level); <br>$Contents = substr($Contents, 0, strlen($Contents) - 4); <br>print $Contents; <br>print pack(</p></span><span>\'</span><span>V</span><span>\'</span><span>,$Crc); <br>print pack(</span><span>\'</span><span>V</span><span>\'</span><span>,$Size); <br>exit; <br>}else{ <br>ob_end_flush(); <br>exit; <br>} <br>} <br>?> <br></span></span>
以上就介绍了 用PHP的ob_start;控制您的浏览器cache!,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。