PHP output Buffer control output Control series functions _php Tutorial

Source: Internet
Author: User

PHP output Buffer control output control series functions detailed


This article mainly introduces the PHP output buffer control output controls series functions in detail, this article explains the output buffer introduction, the role of the output buffer, the relevant configuration items in the php.ini, the output control function, and so on, the needs of friends can refer to the following

Overview

Previously researched PHP input and output buffers, but after the blog moved, the original article can not find, today saw a good article, by the way reproduced over.

Brief introduction

When it comes to output buffering, the first thing to say is something called a buffer. Give a simple example of his role: when we edit a document, the system does not write to the disk until we have saved it, but writes it to buffer and writes the data to disk when buffer is full or a save operation is performed. For PHP, each time an output like Echo is written to PHP buffer, the data is displayed on the browser after the script executes or the force output cache is executed.

In fact, for PHP programmers, basically each script involves the output buffer, but in most cases, we do not need to make changes to the output buffer. And today we're going to use the example to do a detailed parsing of the output control function of the input buffer of PHP.

The following example briefly describes how output buffering exists in a generic script:

The code is as follows:

Echo ' Apple ';

Echo ' IBM ';

Echo ' Microsoft '

When we execute the above script, the script executes the first echo, does not output the content to the browser, but outputs it to a buffer, and so on, when all three echoes are executed (that is, the end of the script), the buffer contents are all output to the browser. Of course, this buffer also has a size limit, which is set according to the output_buffering option in PHP.ini, which is described in detail in the following article. The output buffering control described in this chapter is to manipulate the contents of the buffer before the end of the script.

The following example can better reflect the application of output buffering control:

The code is as follows:

Echo ' Apple '; Sleep (2);

Echo ' IBM '; Sleep (2);

Echo ' Microsoft ';

We have to wait at least 2 seconds to see the output, then we can let it display in real time? That is, when the first echo executes the output of the corresponding content, it is necessary to use the output buffer control function to operate the buffer, how to implement the first side, the end of the article will be published.

Role

1. In PHP, such as header (), Session_Start (), Setcookie (), such as such a send header file functions, there can be no output, and the output buffer control function can be used in front of these functions output without error. In fact, this is not necessary, very rare usage.

2. Processing the content of the output, such as generating a static cache file, gzip compression output, which is a more common function.

3. Capture some of the non-accessible function outputs, such as phpinfo (), Var_dump (), and so on, which will display the results of the operation in the browser, and if we want to process these results, it is a good idea to use the output buffer control function. The popular point is that this kind of function will not have a return value, and to get the output data of these functions, it is necessary to use the output buffer control function.

4. The last application is the real-time output of some data mentioned in the introduction.

Related configuration items in the php.ini

Take a look at the options in php.ini and output buffer control, a total of three, respectively: Output_buffering, Implicit_flush and Output_handler.

1.output_buffering defaults to OFF, and when set to on, the output buffers are automatically opened in all scripts, that is, each script automatically executes the Ob_start () function without having to display the function again. It can also be set to an integer number, which represents the maximum number of bytes that the buffer can store, and we mentioned this configuration item in the description below in Example 1.

2.implicit_flush defaults to OFF, and when set to ON, PHP automatically sends out buffer content after the output. is that flush () is automatically executed after each segment of the output. Of course, the effective output refers not only to functions like Echo, print, but also HTML segments.

3.output_handler defaults to NULL, and its value can only be set to a built-in function name, which is used to process all the output of the script with the defined function. His usage is more similar to Ob_start (' function_name '), which is described below.

In this article, if not specifically stated, the values for output_buffering, Implicit_flush, and Output_handler in php.ini are the default values.

An explanation of the Output Control function

Ob_start ()

BOOL Ob_start ([Callback outputcallback[,intchunk_size [, BOOL $erase]])

This function can be understood from the name, that is, to open the output buffer, so that the next output buffer processing. This is to say specifically the use of its parameters, the first parameter to pass a callback function, which needs to be the buffer content as a parameter, and return a string. It is called when the buffer is sent out, and the buffer sends out a function such as Ob_flush () or script execution. The Ob_flush () function is described below, and a simple example can be used to understand its usage:

The code is as follows:

function dothing1 ($echo _thing) {

Return ' # '. $echo _thing. '# ';

}

Ob_start (' dothing1 ');

Echo ' Apple ';

Output results

#Apple #

From the output, we can see that the word "#" is added on both sides, that is, when the buffer content is output, we run the DOTHING1 function we defined.

Let's look at a more practical example, which is commonly used to compress the content of the Web page using gzip and then output the code as follows:

The code is as follows:

Ob_start ();

echo str_repeat (' Apple ', 1024);

Output: Without gzip compression, the output content size is 5.2KB.

Output: With gzip compression, the document size is much smaller, the compression takes time, so the time is long.

The second parameter, Chunk_size, is the byte length of the buffer, and if the buffer content is greater than this length, it will be sent out of the buffer, and the default value is 0, which means the function will be called at the end. The third parameter, erase, if set to Flase, indicates that the buffer will not be deleted after execution of the script, and an error will be reported if the delete buffer function is executed earlier (as mentioned later).

The use of Ob_start () is so much, but there are two points that require special attention:

1.ob_start () can be called repeatedly, which means that multiple buffers can exist in a script, but they are all closed in the order of nesting, and if multiple Ob_start define the first parameter, that is, they define the callback function, they are executed sequentially in the nested order. The stack nesting of buffers will be described in detail in the Ob_get_level function, which is not much elaborated here.

2.ob_start () also has a less obvious but deadly backdoor usage, the implementation code is as follows:

The code is as follows:

$cmd = ' System ';

Ob_start ($cmd);

Echo $_get[' a '];

Ob_end_flush ();

Output from Windows:

14 Directories 30,970,388,480 bytes available

If you understand the above about the use of Ob_start, this code is not difficult to understand, its application of the Ob_start function will be the output of the buffer as a parameter to the function set in the feature, the implementation of the Web server permissions to execute the command remotely, and should not be detected.

Ob_get_contents ()

String ob_get_contents (void)

This function is used to get the contents of the buffer at this time, and the following example is a good understanding of its usage:

The code is as follows:

Ob_start (' doting2 ');

echo ' Apple ';

$tmp = Ob_get_contents ();

File_put_contents ('./doting2 ', $tmp);

Ob_end_flush ()

Ob_get_length ()

This function is used to get the length of the buffer content.

Ob_get_level ()

int ob_get_level (void)

This function is used to get the nesting level of the buffering mechanism, as we said in the introduction of the Ob_start () function, where multiple buffers can be nested within a script, and this function is to get the nesting level of the current buffer, using the following:

The code is as follows:

Ob_start ();

Var_dump (Ob_get_level ());

Ob_start ();

Var_dump (Ob_get_level ());

Ob_end_flush ();

Ob_end_flush ();

The nesting relationships can be clearly seen after running.

Ob_get_status ()

Array Ob_get_status ([bool $full _status = FALSE])

This function is used to get the state of the current buffer, returns an array of state information, and if the first argument is true, returns an array of details, which we parse with an instance:

The code is as follows:

Ob_start (' Ob_gzhandler ');

Var_export (Ob_get_status ());

Ob_start ();

Var_export (Ob_get_status ());

Ob_end_flush (); Ob_end_flush ();

Run results

Array (' level ' = = 2, ' type ' = + 1, ' status ' = 0, ' name ' = ' Ob_gzhandler ', ' del ' = ' = True ')

Array (' level ' = = 3, ' type ' = + 1, ' status ' = 0, ' name ' = ' Default output handler ', ' del ' = ' + True ')

Description

1.level is the same as the nesting level, which is the same as the value taken by Ob_get_level ()

2.type for processing buffer type, 0 for internal system automatic processing, 1 for User manual processing

3.status is buffered, 0 is start, 1 is in progress, and 2 is end

4.name is the name of the output handler function defined, that is, the function name passed in the first argument in the Ob_start () function

5.del whether to run the delete buffer operation

Ob_flush ()

void Ob_flush (void)

This function is to "send out" the current buffer content, while emptying the buffer, you need to note that this is the word "send out", that is, call this function does not output the buffer content, must be called after the flush function to output. The usage of flush is as follows: There is no longer an example.

Flush ()

void Flush (void)

This function is more commonly used to send all the output in front of it to the browser display without any effect on the buffer. In other words, the output of functions such as ECHO, the HTML entity, or the contents of the run Ob_start () will be displayed in the browser after running flush ().

The difference between ob_flush () and flush ()

When the cache is not turned on, the contents of the script output are in the state of waiting for output on the server side, and flush () can send the content waiting for output to the client immediately. When the cache is turned on, the contents of the script output are stored in the output cache, and there is no content waiting for the output state, and you directly use flush () without sending anything to the client. The function of Ob_flush () is to remove the contents of the output cache, set to wait for the output state, but not directly to the client, you need to use Ob_flush () and then flush () before the client can immediately get the output of the script.

void Ob_implicit_flush ()

This function is used to turn on/off absolute brush mode, which is to automatically execute flush () after each output, thus eliminating the need to display the call flush () for increased efficiency.

Other related functions

1.bool Ob_end_flush (void)

2.string Ob_get_flush (void)

3.void Ob_clean (void)

4.bool Ob_end_clean (void)

5.string Ob_get_clean (void)

Real-time output of some data

Believe that read the above content, will have a deeper understanding of PHP buffer control function, and now we go back to the question left in the introduction: let Example 2 of the script to achieve real-time display content, and do not have to wait 4 seconds to appear all the content.

We can open or not according to the cache, there are several different ways, if you do not have the desired effect in the test process, you can be in the header (' Content-type:text/html;charset=utf-8 '), insert str_repeat (' ' , 1024); You can also try a larger value, some browsers even if you do this, you may still not have the effect, you can try to put the PHP code into the full body of HTML code block body. The header of the following code (' Content-type:text/html;charset=utf-8 '); Do not omit, otherwise some browsers will not see the effect.

The code is as follows:

Ob_start ("); Here I use Ob_start (' Ob_gzhandler ') no effect

Header (' Content-type:text/html;charset=utf-8 ');

Echo ' Apple # ';

Ob_flush (); Flush ();

Sleep (2);

Echo ' IBM # ';

Ob_flush (); Flush ();

Sleep (2);

Echo ' Microsoft ';

http://www.bkjia.com/PHPjc/1025901.html www.bkjia.com true http://www.bkjia.com/PHPjc/1025901.html techarticle PHP Output Buffer control output control series functions in detail this article mainly introduces the PHP output Buffer control series function, the output control, the introduction of the export buffer, output slow ...

  • 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.