PHP ob_start of control browser cache generate HTML implementation code

  • 2020-03-31 20:18:49
  • OfStack

The output control function does not affect the header information sent using header() or setcookie(), but only blocks of data like echo() and PHP code.

Let's take a simple example to give you a general impression of Output Control:
Example 1.
The PHP code
 
<? 
ob_start(); //Open buffer
echo "Hellon"; //The output
header( " location:index.php " ); //Redirect your browser to index.php
ob_end_flush();//Output all content to the browser
?> 

Anyone who knows anything about the header() function knows that it sends a header to the browser, but if there is any output (including empty output such as Spaces, carriage returns, and line feeds) before using the function, it prompts for an error. If we remove ob_start() from the first line and execute the program again, we'll find an error: "Header had all ready send by"! But with ob_start, you don't get an error because when the buffer is opened, the characters after the echo are not output to the browser, but remain on the server until you use flush or ob_end_flush, so there is no error in the output of the file header!
I. introduction of related functions:
1. Flush: Flush the contents of the buffer and output.
Function format: flush()
This function is often used, high efficiency.
2. Ob_start: open the output buffer
Void ob_start(void)
Note: when the buffer is active, all non-file header information from the PHP program is not sent, but is stored in the internal buffer.
To output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
Ob_get_contents: returns the contents of the internal buffer.
String ob_get_contents(void)
Note: this function returns the contents of the current buffer, or FALSE if the output buffer is not active.
Ob_get_length: returns the length of the internal buffer.
Int ob_get_length(void)
Description: this function will return the length of the current buffer; Just like ob_get_contents, if the output buffer is not active. Returns FALSE.
5. Ob_end_flush: sends the contents of the internal buffer to the browser and closes the output buffer.
Method: void ob_end_flush(void)
Description: this function sends the contents of the output buffer, if any.
Ob_end_clean: deletes the contents of the internal buffer and closes the internal buffer
Use method: void ob_end_clean(void)
Note: this function will not output the contents of the internal buffer but delete it!
7. Ob_implicit_flush: turn absolute flush on or off
Method: void ob_implicit_flush ([int flag])
Note: anyone who has ever used Perl knows the meaning of $ │ =x, which turns on/off the buffer, and the ob_implicit_flush function, which, like that, turns off the buffer by default, and when absolute output is turned on, each script output is sent directly to the browser, eliminating the need to call flush().
Ii. In-depth understanding:
1. About Flush function:
This function appears in PHP3, is a very efficient function, it has a very useful function is to refresh the browser's cache.
Example 2.
The PHP code
 
<? 
for($i = 1; $i <= 300; $i++ ) print( "   " ); 
//The key to this is that the structure of a cache is such that the contents of the cache must be of a certain size to be output from the browser
//In other words, if the contents of the cache do not reach a certain size, it will not be printed before the program is finished executing. the
//On test, I found that the size limit is 256 characters long. This means that the cache will receive all of the contents in the future
//A steady stream is being sent out.
For($j = 1; $j <= 20; $j++) { 
echo $j. "  
 " ; 
flush(); //This section pushes out the cache's new content and displays it in the browser
sleep(1); //Let the program "sleep" for a second, will let you see the effect more clearly
} 
?> 

Note: if you add ob_implicit_flush() to the beginning of your program and turn on absolute flush(), you can no longer use flush() in your program.
2. About ob series functions:
I'd like to start with an example from my good friend y10k:
Example 3.
For example, if you want to save the output of the phpinfo() function, what if you want to save the output of the phpinfo() function? Before there is no buffer control, it can be said that there is no way, but with the buffer control, we can easily solve:
The PHP code
 
<? 
ob_start(); //Open buffer
phpinfo(); //Use the phpinfo function
$info=ob_get_contents(); //Gets the contents of the buffer and assigns it to $info
$file=fopen('info.txt','w'); //Open the file info.txt
fwrite($file,$info); //Write the information to info.txt
fclose($file); //Close the file info.txt
?> 

With the above method, you can save the phpinfo information of different users, which may not be possible before! In fact, the above is some of the "process" into the "function" method!
Now that you have some idea of what ob_start() can do, one of the examples above may seem simple, but you've actually got the gist of using ob_start().
< 1 > Open the browser's cache with ob_start. This ensures that the contents of the cache will not be output until you call flush(),ob_end_flush() (or when the program is finished).
< 2 > Now you should know the advantages you have: you can use header,setcookie and session after any output content, which is a big feature of ob_start; You can also use the ob_start parameter to run the command automatically after the cache is written, such as ob_start("ob_gzhandler"); The most common way to do this is to use ob_get_contents() to get the contents of the cache and then process them...
< 3 > When the processing is complete, we can output with a variety of methods, flush(),ob_end_flush(), and automatic output when the program is finished. Of course, if you use ob_get_contents(), then you have to control the output.
Next, let's see what we can do with the ob series of functions...
One, static template technology
Summary: the so-called static template technology is a way for the user to get HTML pages generated by PHP on the client side. If the HTML page is not updated, then the program will no longer call PHP and the related database when another user browses the page again, for some sites with large amount of information, such as sina,163,sohu. The benefits of technologies like this are enormous.
There are two ways to achieve static output that I know of:
< 1 > . Through y10k modification of phplib called template. Inc. PHP class implementation.
< 2 > . Using ob series functions.
As for the first method, it is not the subject of this article, so it will not be repeated.
Now let's take a look at the implementation of the second method:
Example 4.
The PHP code
 
<?php 
ob_start();//Open buffer
?> 

The entire output of the PHP page
The PHP code
 
<? 
$content = ob_get_contents();//Gets the entire output of the PHP page
$fp = fopen("output00001.html", "w"); //Create a file, open it, and prepare to write
fwrite($fp, $content); //Write the entire contents of the PHP page to output001.html, and then...
fclose($fp); 
?> 

In this way, so-called static templates can be easily implemented...
Capture output
Example 4 above is the simplest case. You can also do something with $content before writing...
You can try to capture some keywords and then reprocess them, such as Example 3. The PHP syntax described here is highlighted. Personally, this feature is the best part of the function. It can solve all kinds of problems, but you need to have enough imagination...
Example 5.
The PHP code
 
<? 
Function run_code($code) { 
If($code) { 
ob_start(); 
eval($code); 
$contents = ob_get_contents(); 
ob_end_clean(); 
}else { 
echo " Error! There is no output "; 
exit(); 
} 
return $contents; 
} 
?> 

The above example is not very useful, but it is typical that $code itself is an output page containing variables, and this example USES eval to replace the variables in $code, and then carries out output capture on the output results, and processes again...
Example 6. Speed up transmission
The PHP code
 
<? 
/* 
** Title.........: PHP4 HTTP Compression Speeds up the Web 
** Version.......: 1.20 
** Author........: catoc <[email]catoc@163.net[/email]> 
** Filename......: gzdoc.php 
** Last changed..: 18/10/2000 
** Requirments...: PHP4 >= 4.0.1 
** PHP was configured with --with-zlib[=DIR] 
** Notes.........: Dynamic Content Acceleration compresses 
** the data transmission data on the fly 
** code by sun jin hu (catoc) <[email]catoc@163.net[/email]> 
** Most newer browsers since 1998/1999 have 
** been equipped to support the HTTP 1.1 
** standard known as "content-encoding." 
** Essentially the browser indicates to the 
** server that it can accept "content encoding" 
** and if the server is capable it will then 
** compress the data and transmit it. The 
** browser decompresses it and then renders 
** the page. 
** 
** Modified by John Lim ([email]jlim@natsoft.com.my[/email]) 
** based on ideas by Sandy McArthur, Jr 
** Usage........: 
** No space before the beginning of the first '<?' tag. 
** ------------Start of file---------- 
** |<? 
** | include('gzdoc.php'); 
** |? > 
** |<HTML> 
** |... the page ... 
** |</HTML> 
** |<? 
** | gzdocout(); 
** |? > 
** -------------End of file----------- 
*/ 
ob_start(); 
ob_implicit_flush(0); 
function CheckCanGzip(){ 
global $HTTP_ACCEPT_ENCODING; 
if (headers_sent() || connection_timeout() || connection_aborted()){ 
return 0; 
} 
if (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false) return "x-gzip"; 
if (strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false) return "gzip"; 
return 0; 
} 
 
function GzDocOut($level=1,$debug=0){ 
$ENCODING = CheckCanGzip(); 
if ($ENCODING){ 
print "n<!-- Use compress $ENCODING -->n"; 
$Contents = ob_get_contents(); 
ob_end_clean(); 
if ($debug){ 
$s = "<p>Not compress length: ".strlen($Contents); 
$s .= " 
Compressed length: ".strlen(gzcompress($Contents,$level)); 
$Contents .= $s; 
} 
header("Content-Encoding: $ENCODING"); 
print "x1fx8bx08x00x00x00x00x00"; 
$Size = strlen($Contents); 
$Crc = crc32($Contents); 
$Contents = gzcompress($Contents,$level); 
$Contents = substr($Contents, 0, strlen($Contents) - 4); 
print $Contents; 
print pack('V',$Crc); 
print pack('V',$Size); 
exit; 
}else{ 
ob_end_flush(); 
exit; 
} 
} 
?> 

This is catoc a long time ago of the code, is in weblogs.com to see, he used zlib function, the content of the transmission of compression, testing shows that, for 10k more than the page, will have an effect, and the larger the page, the more obvious the effect...

Related articles: