Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does PHP get the contents of the output buffer

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to get the contents of the output buffer in PHP". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to get the contents of the output buffer by PHP.

Output buffer Control in PHP

In PHP, when we do echo or print_r directly, the output will be printed directly. However, in some cases, we do not want to print directly, so we can use the output buffer control to control the output printing. Of course, this set of functions is not limited to print content, we can also do some other operations, which we will talk about at the end.

Clear output

First of all, let's take a look at not letting things like echo be printed out.

Ob_start ()

Echo 111, PHP_EOL

Echo "aaaa", PHP_EOL

Ob_end_clean ()

I believe that many friends should have seen ob_start () this function, its function is to start a section of output buffer control. The output statements in the code after ob_start () enter the output buffer, and at this point, if we call ob_end_clean (), ob_clean (), or ob_get_clean (), there will be no output. The purpose of all three is to clear the contents of the output buffer. For specific differences, you can refer to the function description or official documentation given at the end of the article.

Get the contents of the output buffer ob_start ()

Echo 111, PHP_EOL

Echo "aaaa", PHP_EOL

$v = ob_get_contents ()

Ob_end_clean ()

Echo $v

As mentioned above, using ob_end_clean () clears the contents of the output buffer, but in this code, we use the ob_get_contents () function to directly assign the contents of the buffer to the variable\ $v. At this point, we have the contents of the first two paragraphs of echo in $v, that is, this set of operations takes what we should output and stores it in a variable. What's the use of this? We can get the contents of direct output functions such as phpinfo () and var_dump (), and will not print them on the client screen. For example:

Ob_start ()

Php_info ()

$v = ob_get_contents ()

Ob_end_clean ()

Echo $v

The content in $v is the content of php_info (). This is the second ability of output buffering control.

Refresh (output) buffer contents ob_start ()

Echo 111, PHP_EOL

Echo "aaaa", PHP_EOL

Flush ()

Ob_flush ()

Similarly, if we want to output content directly again in the buffer, we can just use flush (), ob_flush (), ob_end_flush (), and ob_get_flush (), which is the equivalent of letting output statements such as echo after ob_start () take effect again and output normally.

In addition, we can also use a function for automatic refresh.

Ob_implicit_flush ()

Ob_start ()

Echo 111, PHP_EOL

Echo "aaaa", PHP_EOL

After using ob_implicit_flush (), we don't need to manually call functions such as ob_flush () to flush the buffer contents.

Some detection functions ob_start ()

Ob_start ()

Echo 123, PHP_EOL

Echo ob_get_length (), PHP_EOL

/ / 3

Echo ob_get_level (), PHP_EOL

/ / 2

Print_r (ob_get_status (true))

/ / Array

/ / (

/ / [0] = > Array

/ / (

/ / [name] = > default output handler

/ / [type] = > 0

/ / [flags] = > 112

/ / [level] = > 0

/ / [chunk_size] = > 0

/ / [buffer_size] = > 16384

/ / [buffer_used] = > 0

/ /)

/ / [1] = > Array

/ / (

/ / [name] = > default output handler

/ / [type] = > 0

/ / [flags] = > 112

/ / [level] = > 1

/ / [chunk_size] = > 0

/ / [buffer_size] = > 16384

/ / [buffer_used] = > 17

/ /)

/ /)

Ob_get_flush ()

Ob_get_length () returns the length of the contents of the current buffer. Here we only print a 123 and save three characters in the buffer, so the output is exactly 3. Ob_get_level () returns the level of the current buffer. Notice that we called ob_start () twice above, that is, there are two layers of buffers that can be nested. The ob_get_status () function is the status information of the buffer, and the description of the field can be viewed in the official documentation, so I won't repeat it here.

Use the callback function of ob_start () to replace the contents of the output buffer

This is an example, but it can be extended to other special functions, such as global output filtering, compression optimization of CSS or JS files, and so on.

Ob_start (function ($text) {

Return (str_replace ("apples", "oranges", $text))

});

Echo "It's like comparing apples to oranges", PHP_EOL

Ob_get_flush ()

/ / It's like comparing oranges to oranges

The final output is to replace the apples content with the oranges content.

Add URL rewriter output_add_rewrite_var ('var',' value')

/ / some links

Echo 'link

Link2'

/ / link

/ / link2

/ / a form

Echo'

'

/ /

/ /

/ /

/ /

What do you see from the above code? Yes, using the output_add_rewrite_var () function, we can add a parameter to the HTML link or form code when PHP is output. Have you thought of any usage scenarios? Prevention of CSRF attacks on POST forms.

This function is added according to the url_rewriter.tags configuration item in the php.ini file. By default, this configuration item only supports from forms. At the same time, it can also support a tag href, area tag href, frame tag src, input tag src, and so on. That is, fields are automatically added to the attributes corresponding to these tags. Of course, it also has an inverse function output_reset_rewrite_vars () to cancel the previously added parameter.

At this point, I believe you have a deeper understanding of "how to get the contents of the output buffer in PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report