Summary of common functions in PHP OB series functions

Source: Internet
Author: User
Tags file info php class phpinfo setcookie
Functions commonly used in OB series functions


Ob_start (); Open an output buffer, and all output information is not sent directly to the browser, but is stored in the output buffer.

Ob_clean (); Deletes the contents of the internal buffer without closing the buffer (no output).
Ob_end_clean (); Delete the contents of the internal buffer and close the buffer (not output).
Ob_get_clean (); Returns the contents of the internal buffer, closing the buffer. Equivalent to executing ob_get_contents () and Ob_end_clean ()
Ob_flush (); Sends the contents of the internal buffer to the browser, deleting the contents of the buffer and not closing the buffer.
Ob_end_flush (); Sends the contents of the internal buffer to the browser, deletes the contents of the buffer, and closes the buffer.
Ob_get_flush (); Returns the contents of the internal buffer, closes the buffer, and then releases the contents of the buffer. Equivalent to Ob_end_flush () and returns the buffer contents.
Flush (); Outputs the contents of the Ob_flush, as well as the content not in the PHP buffer, to the browser, refreshes the contents of the internal buffer, and outputs.

Ob_get_contents (); Returns the contents of the buffer, not output.
Ob_get_length (); Returns the length of the internal buffer, which returns false if the buffer is not activated.
Ob_get_level (); Return The nesting level of the output buffering mechanism.
Ob_get_status (); Get status of output buffers.

Ob_implicit_flush (); Turn absolute refresh on or off, default is off, turn on Ob_implicit_flush (true), the so-called absolute refresh, that is, when an output statement (E.g:echo) is executed, the output is sent directly to the browser, and no need to call flush () Or wait until the end of the script to output.

Ob_gzhandler the//ob_start callback function to compress the contents of the buffer with gzip.
Ob_list_handlers//list all output handlers on use
Output_add_rewrite_var//add URL rewriter values
Output_reset_rewrite_vars//reset URL rewriter values

The behavior of these functions is affected by the Php_ini setting:
Output_buffering//This value is on when output control is used in all scripts, and if the value is a number, it represents the maximum byte limit of the buffer, and when the cache content reaches that limit it will automatically output the contents of the current buffer to the browser.
Output_handler//This option redirects all output of the script to a function. For example, when Output_handler is set to Mb_output_handler (), the encoding of the character is modified to the specified encoding. Any processing function that is set will automatically process the output buffer.
Implicit_flush//function with Ob_implicit_flush, default is off.

Use PHP's Ob_start ();
Control your browser cache


The output control function gives you the freedom to control the outputs of the data in your script. It is very useful, especially when you want to output a file header after the data has been output. The output control function does not affect the header information sent using header () or Setcookie (), only data blocks that resemble Echo () and PHP code are useful.
Let's start with a simple example that gives you a general impression of the output control:
Example 1.

Program Code Program code
Ob_start (); Open buffer
echo \ "Hellon\"; Output
Header ("location:index.php"); redirect the browser to index.php
Ob_end_flush ();//output all content to the browser
?>


All people who know about the header () function know that this function sends a file header to the browser, but if there are any outputs (including null output, such as spaces, carriage returns, and line feeds) before using this function, an error is indicated. If we get rid of the first line of Ob_start () and execute the program, we'll find an error message: "Header had all ready to send by"! But with Ob_start, there is no hint of error, because when the buffer is opened, the character behind the echo is not output to the browser, but remains on the server until you use flush or ob_end_flush to output, so there will be no file header output error!


Introduction of related functions:
1, Flush: Flush the contents of the buffer, output.
function format: Flush ()
Description: This function is often used and is highly efficient.
2. Ob_start: Open Output buffer
function format: void Ob_start (void)
Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.
3. Ob_get_contents: Returns the contents of the internal buffer.
How to use: String ob_get_contents (void)
Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.
4. Ob_get_length: Returns the length of the internal buffer.
How to use: int ob_get_length (void)
Note: This function returns the length of the current buffer, as with ob_get_contents, if the output buffer is not activated. FALSE is returned.
5. Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer.
How to use: void Ob_end_flush (void)
Description: This function sends the contents of the output buffer (if any).
6. Ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
How to use: void Ob_end_clean (void)
Description: This function does not output the contents of the internal buffer but deletes it!
7. Ob_implicit_flush: Turn absolute refresh on or off
How to use: void Ob_implicit_flush ([int flag])
Description: People who have used Perl know the meaning of $|=x, this string can open/close the buffer, and the Ob_implicit_flush function is the same, the default is to close the buffer, open the absolute output, each script output is sent directly to the browser, no longer need to call flush ()


Second, in-depth understanding:

1. About the Flush function:
This function appears in PHP3, is a very efficient function, and he has a very useful function is to refresh the browser cache. Let's give an example of a very obvious running effect that illustrates flush.
Example 2.

Program Code Program code
for ($i = 1; $i <=, $i + +) print ("");
This sentence is very key, the structure of the cache so that its content can only reach a certain size to be exported from the browser
In other words, if the content of the cache does not reach a certain size, it will not be output until the program finishes executing. By
After testing, I found that the bottom limit of this size is 256 characters in length. This means that content received by the cache will be
The stream was sent out.
for ($j = 1; $j <=; $j + +) {
echo $j. "
";
Flush (); This will cause the cache additions to be squeezed out and displayed on the browser
Sleep (1); Let the program "Sleep" a second, will let you see the effect more clearly
}
?>

Note: If you open an absolute refresh by adding Ob_implicit_flush () to the program's header, you can no longer use flush () in your program, and the benefit is: increase efficiency!

2. About OB series functions:
I'd like to cite an example of my good friend y10k first:
Example 3.

For example, you use the server and client settings information, but this information is different from the client, if you want to save the Phpinfo () function output what to do? Before there is no buffer control, it can be said that there is no way, but with the control of the buffer, we can easily solve:
Program Code Program code
Ob_start (); Open buffer
Phpinfo (); Using the Phpinfo function
$info =ob_get_contents (); Get the contents of the buffer area and assign it to $info
$file =fopen (\ ' info.txt\ ', \ ' w\ '); Open File Info.txt
Fwrite ($file, $info); Write information to Info.txt
Fclose ($file); Close File Info.txt
?>

Using the above method, you can save the Phpinfo information of different users, which in the past I am afraid there is no way to do! In fact, the above is a few "process" into the "function" Method!
One might ask, "is that what it looks like?" Is there any other use? "Of course, such as the author of the Forum's PHP syntax highlighting is related to this (php default syntax highlighting function will be directly output, can not save the results, if every call is likely to be a waste of CPU, the author's forum to the syntax highlighting function display results with the method of control buffer), You can take a look if you're interested.

Perhaps now you have a certain understanding of the function of Ob_start (), the above example seems simple, but actually has mastered the use of Ob_start () points.
<1>: Use Ob_start to open the browser cache, which guarantees that the contents of the cache will not be output until you call Flush (), Ob_end_flush () (or the program finishes).
<2> Now you should know the advantages you have: You can use the Header,setcookie and session after any output, which is a great feature of Ob_start, or you can use the Ob_start parameters after the cache is written , and then run the command automatically, such as Ob_start (\ "Ob_gzhandler\"), and our most common practice is to use ob_get_contents () to get the contents of the cache and then process it ...
<3> When processing is complete, we can use various methods to output, flush (), Ob_end_flush (), and wait until the program finishes executing the automatic output. Of course, if you use Ob_get_contents (), then you have to control the output mode.

Here, let's see what we can do with OB series functions ...

First, static template technology

Introduction: The so-called static template technology is in some way, so that users on the client side is generated by PHP HTML page. If this HTML page is not updated again, then when another user browses to the page again, the program will no longer invoke PHP and the associated database, for some of the more informative sites, such as Sina,163,sohu. The benefits of a technology like this are enormous.

There are two ways I know to implement static output:
<1>. A template.inc.php class implementation that modifies Phplib by y10k.
<2> Use the OB series function implementation.
For the first method, because it is not the issue to be studied in this article, so don't repeat it.
Let's take a look at the concrete implementation of the second approach:
Example 4.


Program Code Program code
Ob_start ();//Open buffer
?>
Full output of PHP page
$content = Ob_get_contents ();//Get all the contents of the PHP page output
$fp = fopen ("output00001.html", "w"); Create a file, and open it, ready to write
Fwrite ($fp, $content); Write the contents of the PHP page to output00001.html, then ...
Fclose ($FP);
?>


In this way, the so-called static template is easily implemented ...

Second, capture the output

The above example 4. Is the simplest case, you can also work on the $content before writing ...

You can try to catch some keywords and then go back to it, such as Example 3. The PHP syntax is highlighted. Personally think that this function is the biggest essence of this function, it can solve a variety of problems, but need you have enough imagination ...

Example 5.

Program Code Program code
Function Run_code ($code) {
If ($code) {
Ob_start ();
eval ($code);
$contents = Ob_get_contents ();
Ob_end_clean ();
}else {
echo "Error! No output ";
Exit ();
}
return $contents;
?>
}

The use of this example is not very large, but the typical $code itself is a variable output page, and this example uses eval to replace the variables in the $code, and then the output to capture the output, and then once the processing ...

Two, output cache handle Ob_gzhandler


PHP4.0.4 has a new output cache handle Ob_gzhandler, similar to the previous class, but with a different usage. The content to be added to PHP.ini when using Ob_gzhandler is as follows:


Output_handler = Ob_gzhandler;


This line of code allows PHP to activate the output cache and compress everything it sends out.

If for some reason you do not want to add this line of code to php.ini, you can also change the default server behavior (not compressed) by using the. htaccess file in the directory where the PHP source files are located, with the following syntax:


Php_value Output_handler Ob_gzhandler


Or it is called from the PHP code, as follows:


Ob_start ("Ob_gzhandler");


The method of using output cache handles is really effective and does not give the server any special load. It is important to note, however, that Netscape Communicator has poor support for compressed graphics, so you should suppress JPEG and GIF graphics unless you can ensure that all users are using Internet Explorer. Generally, this compression works for all other files, but it is recommended that you test separately for various browsers, especially if you are using a special plug-in or data viewer.
Precautions:
1, some Web server output_buffering default is 4069 characters or larger, that is, the output must reach 4069 character server to flush the output buffer, in order to ensure that flush is valid, it is best to have the following statement before the Ob_flush () function:

The code is as follows:

Print Str_repeat ("", 4096); To ensure that the output_buffering value is reached


2, Ob_* series function is to operate the output buffer of PHP itself, so ob_flush only refresh PHP's own buffer, and flush is the buffer to flush Apache. Therefore, the correct use of the two order is: first Ob_flush, and then flush. Ob_flush is to release the data from the PHP buffer, flush is to send the buffer inside/out all the data to the browser.
3, do not mistakenly think that after using Ob_start (), the script Echo/print output will never be displayed on the browser. Because the PHP script finishes running, it automatically refreshes the buffer and outputs the content.

The above describes the PHP OB series functions commonly used in the summary of functions, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.