[Reprint] Original address: http://www.itbbs.cn/index.php?showtopic=1074
The Output Control function allows you to freely control the output of data in the script. It is very useful, especially when you want to output the file header after the data has been output. The output control function does not affect the file header information sent using header() or setcookie(), only the data blocks similar to echo() and PHP code.
Let’s give a simple example first to give everyone a general impression of Output Control:
Example 1.
<span><br> <span><?php <BR>ob_start</SPAN><SPAN>(); </SPAN><SPAN>//Open buffer <BR></SPAN><SPAN>echo </SPAN><SPAN>"Hellon"; //Output <BR>header("</SPAN><SPAN>location</SPAN> <SPAN>:</SPAN><SPAN>index</SPAN> <SPAN>.</SPAN><SPAN>php</SPAN><SPAN>"); //Redirect the browser to index.php <BR>ob_end_flush();//Output all the content to the browser <BR>?> <br> </span></span>
<span><br><span><?php <BR>$i</span><span>++ ) print (</span><span>" "</span><span>); </span><span></span>// This sentence is very critical. The structure of the cache makes it so that its content can only be output from the browser when it reaches a certain size. <span>// In other words, if the content of the cache does not reach a certain size, The size will not be output before the program execution is completed. After </span>// testing, I found that the lower limit of this size is 256 characters long. This means that the content received by the cache in the future will <span>// be sent out continuously. </span><span></span>For(<span></span>$j <span></span>= <span></span>1<span></span>; <span></span>$j <span><br><= </SPAN><SPAN>20<BR><BR>; <BR><BR>$j</SPAN><SPAN>++) { </SPAN>echo <SPAN></SPAN>$j<SPAN> </SPAN>.<SPAN></SPAN>" <SPAN>"</SPAN><SPAN>; </SPAN><SPAN></SPAN>flush<SPAN></SPAN>(); <SPAN></SPAN>//This part will squeeze out the new content from the cache and display it on the browser <SPAN></SPAN><SPAN>sleep<BR></SPAN>(<SPAN> </SPAN>1<SPAN></SPAN>); <SPAN><BR>//Let the program "sleep" for a second, which will allow you to see the effect more clearly </SPAN><SPAN><BR>} </SPAN><SPAN></SPAN>?><span> </span><span><br></span><br>Note: If you add ob_implicit_flush() to the beginning of the program to turn on absolute refresh, you can no longer use flush() in the program. The advantage of this is: improve efficiency! <br>2. Regarding the ob series functions: <br>I would like to quote an example from my good friend y10k first: <br>Example 3. <br>For example, you can use the setting information of the server and the client, but this information will be different depending on the client. If What should I do if I want to save the output of the phpinfo() function? Before there was no buffer control, it can be said that there was no solution at all, but with buffer control, we can easily solve it: <br><div class="code">
<code><span><br><span><?php <BR>ob_start</SPAN><SPAN>(); </SPAN><SPAN>//Open Buffer <BR></SPAN><SPAN>phpinfo</SPAN><SPAN>(); </SPAN><SPAN>//Use phpinfo function <BR></SPAN><SPAN>$info</SPAN><SPAN>=</SPAN><SPAN>ob_get_contents</SPAN><SPAN>(); </SPAN><SPAN>//Get the contents of the buffer and assign it to $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>); //Open file info.txt <BR>fwrite( $file, $info); //Write information to info.txt <BR>fclose($file); //Close the file info.txt <BR>?> <br></span></span>
<span><br><span><?php <BR>ob_start</SPAN><SPAN>();</SPAN><SPAN>//Open the buffer <BR></SPAN><SPAN>?></span> <br>All output of php page <br><span><? <BR>$content </SPAN><SPAN>= </SPAN><SPAN>ob_get_contents</SPAN><SPAN>();</SPAN><SPAN>//Get all content of php page output <BR></SPAN><SPAN>$fp </SPAN><SPAN>= </SPAN><SPAN>fopen </SPAN><SPAN>( </SPAN><SPAN>"output00001.html"</SPAN><SPAN>, </SPAN><SPAN>"w"</SPAN><SPAN>); </SPAN><SPAN>//Create a file and open it, ready to write <BR></SPAN><SPAN>fwrite</SPAN><SPAN>(</SPAN><SPAN>$fp</SPAN><SPAN>, </SPAN> <SPAN>$ content</SPAN><SPAN>); </SPAN><SPAN>//Write all the content of the php page into output00001.html, and then... <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>"Error! No output"</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 <[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>
The above has introduced the use of PHP's ob_start; to control your browser cache!, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.