【轉載】原文網址:http://www.itbbs.cn/index.php?showtopic=1074
Output Control 函數可以讓你自由控制腳本中資料的輸出。它非常有用,特別是對於:當你想在資料已經輸出後,再輸出檔案頭的情況。輸出控制函數不對使用 header() 或 setcookie(), 發送的檔案頭資訊產生影響,只對那些類似於 echo() 和 PHP 程式碼的資料區塊有作用。
我們先舉一個簡單的例子,讓大家對Output Control有一個大致的印象:
Example 1.
<span><code><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>
ob_start(); //開啟緩衝區 echo "Hellon"; //輸出 header("location:index.php"); //把瀏覽器重新導向到index.php ob_end_flush();//輸出全部內容到瀏覽器 ?> <span><code><span><br><span><?php <BR></SPAN><SPAN>for(</SPAN><SPAN>$i </SPAN><SPAN>= </SPAN><SPAN>1</SPAN><SPAN>; </SPAN><SPAN>$i </SPAN><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><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>
for($i = 1; $i 300; $i++ ) print(" "); // 這句話非常關鍵,cache的結構使得它的內容只有達到一定的大小才能從瀏覽器裡輸出 // 換言之,如果cache的內容不達到一定的大小,它是不會在程式執行完畢前輸出的。經 // 過測試,我發現這個大小的底限是256個字元長。這表示cache以後收到的內容都會 // 源源不絕的被送出去。 For($j = 1; $j 20; $j++) { echo $j." "; flush(); //這部會讓cache新增的內容被擠出去,顯示瀏覽器上 sleep (1); //讓程式"睡"一秒鐘,會讓你把效果看得更清楚 } ?> <span><code><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>
ob_start(); //開啟緩衝區 <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>
我所知道的實作靜態輸出的有兩種方法: .透過y10k修改的phplib的一個叫template.inc.php類別實作。 .使用ob系列函數實作。 對於第一種方法,因為不是這篇文章所要研究的問題,所以不再贅述。 我們現在來看看第二種方法的具體實作: Example 4. <span><span><?php <🎜>ob_start SPAN><span>();</span><span>//開啟緩衝區 </span><span>?></span> php頁面的全部輸出 <span> $content </span><span>= </span><span>ob_get_contents</span><span>();</span><span>//取得php頁面輸出的全部內容 </span><span>$fp </span><span>= </span><span>fopen</span><span>(</span><span>"output00001.html"</span><span>, </span><span>"w"</span><span>); </span><span>//建立文件,打開,準備寫入 </span> <span>fwrite</span><span>(</span><span>$fp</span><span>, </span><span>$content</span><span>); </span><span>//把php頁面的內容全部寫入output00001.html,然後… </span><span>fclose</span><span>(</span><span>$fp<span>); </span><span>?></span> </span></span></span>
<span><code><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>
Function run_code($code< SPAN>) { <🎜>If($code) { <🎜>ob_start(); <🎜 >eval($code); <🎜>$contents = ob_get_contents< /SPAN>(); <🎜>ob_end_clean(); <🎜>}else { <🎜>echo "錯誤!沒有輸出"; <🎜>exit(); <🎜>} <🎜>return $contents; <🎜>} <🎜> <🎜><SPAN><BR><SPAN><? <BR></SPAN><SPAN>/* <BR>** Title.........: PHP4 HTTP Compression Speeds up the Web <BR>** Version.......: 1.20 <BR>** Author........: catoc <[email]catoc@163.net[/email]> <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) <[email]catoc@163.net[/email]> <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>** |<HTML> <br>** |... the page ... <br>** |</HTML> <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(</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教程有興趣的朋友有所幫助。