PHP buffering output_buffering and Ob_start Introduction _php tutorial

Source: Internet
Author: User
Tags drupal
Buffer

Buffer is a memory address space, the Linux system default size is generally 4096 (4KB), that is, a memory page. It is primarily used to store data between devices that are out of sync or between devices with different priority levels. By using buffer, you can make the process less of a mutual wait. Here is a popular example, when you open a text editor to edit a file, when you enter a character, the operating system does not immediately write this character directly to the disk, but the first write to buffer, when the buffer is filled with a buffer, the data will be written to disk, Of course, when the kernel function flush () is called, it is mandatory to write the dirty data in buffer back to disk.
Similarly, when executing a echo,print, the output is not immediately displayed via TCP to the client browser, but instead writes the data to PHP buffer. The PHP output_buffering mechanism means that a new queue is established before the TCP buffer, and the data must pass through the queue. When a PHP buffer is full, the script process will send the output data from PHP buffer to the system kernel to be displayed by TCP to the browser. So, the data will be written to these places echo/pring. PHP Buffer---browser

PHP output_buffering

By default, PHP buffer is turned on, and the default value of this buffer is 4096, which is 4kb. You can find the output_buffering configuration in the php.ini configuration file. The output data is written to the PHP output_buffering when the user data is Echo,print, until Output_ Buffering is full, this data will be transmitted via TCP to the browser display. You can also manually activate the PHP output_buffering mechanism through Ob_start (), so that even if the output exceeds 4KB data, it does not really give the data to TCP to the browser, because Ob_start () set the PHP buffer space to large enough. Data is sent to the client browser only until the script finishes, or when the Ob_end_flush function is called.
1. When output_buffering=4096, and output less data (less than one buffer)

Copy the Code code as follows:
for ($i = 0; $i < $i + +) {
Echo $i. '
';
Sleep ($i + 1); //
}
?>

Phenomenon: Not every few seconds there will be intermittent output, but until the end of the response, in order to see the output at once, wait until the end of the server script processing, the browser interface remains blank. This is because the amount of data is too small and PHP output_buffering is not full. The order in which the data is written, in turn echo->php buffer->tcp buffer->browser
2. When output_buffering=0, and output less data (less than one buffer)

Copy the Code code as follows:
Through Ini_set (' output_buffering ', 0) does not take effect
You should edit/etc/php.ini, set output_buffering=0 disable output buffering mechanism
Ini_set (' output_buffering ', 0); Completely disable the output buffering feature
for ($i = 0; $i < $i + +) {
Echo $i. '
';
Flush (); Notifies the operating system of the underlying, as soon as possible the data to the client browser
Sleep ($i + 1); //
}
?>

Phenomenon: Inconsistent with the display just now, after disabling the PHP buffering mechanism, the browser can intermittently see the intermittent output, do not have to wait until the completion of the script to see the output. This is because the data is not stuck in PHP output_buffering. The order in which the data is written is Echo->tcp Buffer->browser
3. When output_buffering=4096., output data is greater than one buffer, do not call Ob_start ()
Create a 4kb-sized file
$dd If=/dev/zero of=f4096 bs=4096 count=1

Copy the Code code as follows:
for ($i = 0; $i < $i + +) {
Echo file_get_contents ('./f4096 '). $i. '
';
Sleep ($i + 1);
}
?>

Symptom: The response is not over (the HTTP connection is not turned off), intermittent output is visible, and the browser interface does not remain blank. Although the PHP output_buffering mechanism is enabled, it still intermittently outputs, rather than a one-time output, because output_buffering space is not enough. With each PHP buffering, the data is sent to the client browser. 4. When output_buffering=4096, the output data is greater than a TCP buffer, call Ob_start ()

Copy the Code code as follows:
Ob_start (); Open PHP Buffer
for ($i = 0; $i < $i + +) {
Echo file_get_contents ('./f4096 '). $i. '
';
Sleep ($i + 1);
}
Ob_end_flush ();
?>

Phenomenon: Until the server end of the script processing completed, the response is finished, only to see the full output, the time interval is very short, so that you do not feel the pause. Before the output, the browser kept a blank interface, waiting for server data. This is because, once PHP calls the Ob_start () function, it expands PHP buffer to be large enough to send the data in PHP buffer to the client browser until the Ob_end_flush function call or the script runs at the junction speed.
tcpdump observation
Here, we monitor TCP messages through tcpdump to see the difference between using Ob_start () and not using it.
1. No use of Ob_start ()
12:30:21.499528 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. Ack 485 win 6432 12:30:21.500127 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 1:2921 (2920) Ack 485 win 6432 12:30:21.501000 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 2921:7301 (4380) Ack 485 win 6432 12:30:21.501868 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 7,301:8,412 (1111) ACK 485 win 643 12:30:24.502340 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 8412:14252 (5840) Ack 485 win 6432 12:30:24.503214 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 14252:15712 (1460) Ack 485 win 6432 12:30:24.503217 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 15,712:16,624 (9 Ack 485 win 6432 12:30:31.505934 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 16624:23924 (7300) Ack 485 win 6432 12:30:31.506839 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 23,924:24,836 (9 Ack 485 win 6432 12:30:42.508871 IP 192.168.0.8.webcache > 192.168.0.28.cYmtec-port:. 24836:32136 (7300) Ack 485 win 6432 12:30:42.509744 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 32,136:33,048 (9 Ack 485 win 6432 12:30:57.512137 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 33048:40348 (7300) Ack 485 win 6432 12:30:57.513016 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 40,348:41,260 (9 Ack 485 win 6432 12:31:06.513912 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 41260:41265 (5) ACK 485 win 6 432 12:31:06.514012 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:f 41265:41265 (0) Ack 485 win 6432 12:31:06.5143 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. ACK 486 win 6432
2. Using the Ob_start ()
12:36:06.542244 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. Ack 485 win 6432 12:36:51.559128 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 1:2921 (2920) Ack 485 win 6432 12:36:51.559996 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 2921:7301 (4380) Ack 485 win 6432 12:36:51.560866 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 7301:11681 (4380) Ack 485 win 6432 12:36:51.561612 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 11681:16061 (4380) Ack 485 win 6432 12:36:51.561852 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 16061:20441 (4380) Ack 485 win 6432 12:36:51.562479 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 20441:24821 (4380) Ack 485 win 6432 12:36:51.562743 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 24821:29201 (4380) Ack 485 win 6432 12:36:51.562996 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 29201:33581 (4380) Ack 485 win 6432 12:36:51.563344 IP 192.168.0.8.webcache > 192.168.0.28.noagent:p 33,581:35,041 (1460) Ack 485 win 6432 12:36:51.563514 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 35041:36501 (1460) Ack 485 win 6432 12:36:51.563518 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 36501:37961 (1460) Ack 485 win 6432 12:36:51.563523 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 37961:39421 (1460) Ack 485 win 6432 12:36:51.563526 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 39421:40881 (1460) Ack 485 win 6432 12:36:51.563529 IP 192.168.0.8.webcache > 192.168.0.28.NOAGENT:FP 40,881:41,233 (352) Ack 485 win 6432 12:36:51.570364 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. ACK 486 win 6432
From the above comparison, we can see that the time interval of the data message is obviously different. Not using Ob_start (), the time interval is relatively large, waiting for about 4 seconds to send the data in the TCP buffer. Data is not in PHP buffer for too long, the output data is sent to the client browser. That's because, soon enough, PHP buffer was full and had to send the data out. When Ob_start () is enabled, it is different, sending packets to the client, which is sent almost at the same time. This makes it possible to infer that the data has been in PHP buffer until the Ob_end_flush () is called to send the data in PHP buffer to the client browser.

Output buffering function

1.ob_start

Activates the output_buffering mechanism. Once activated, the script output is no longer directed to the browser, but is temporarily written to the PHP buffer memory area.
PHP opens the output_buffering mechanism by default, except that by calling the Ob_start () function The output_buffering value is extended to large enough. You can also specify $chunk_size to specify the value of Output_buffering. The default value of $chunk _size is 0, which means that data in PHP buffer is not sent to the browser until the end of the script run. If you set the size of the $chunk_size, the data in buffer is sent to the browser as long as the data length in the buffer reaches that value.
Of course, you can work with the data in buffer by specifying $ouput_callback. For example, function Ob_gzhandler, compress the data in buffer and then pass it to the browser.

2.ob_get_contents

Get a copy of the data in PHP buffer. It is worth noting that you should call this function in the Ob_end_clean () function call, otherwise ob_get_contents () returns an empty character.

3.ob_end_flush and Ob_end_clean

These two functions are a bit similar and will close the ouptu_buffering mechanism. But the difference is that Ob_end_flush simply flushes the data in PHP buffer (flush/send) to the client browser, and Ob_clean_clean empties the data in PHP bufeer (erase), but does not send it to the client browser. After the Ob_end_flush call, the data in PHP buffer still exists, and ob_get_contents () can still get a copy of the data in PHP buffer. After the Ob_end_clean () call, Ob_get_contents () takes an empty string, and the browser receives no output, that is, no output.

Usage Cases

often see Ob_start () used in some template engine and page file caches. In the well-known open source project Wordpress,drupal,smarty and other places, can find their shadow. The application of Drupal is drawn here.
Template file

Copy the Code code as follows:
@file: user-profile.tpl.php


    • Username: name;?>

    • Picture: Picture ;?>




@file: template-render.php
function Theme_render_template ($template _file, $variables) {
if (!is_file ($template _file) {return "";}
Extract ($variables, extr_skip);
Ob_start ();
$contents = Ob_get_contents ();
Ob_end_clean ();
return $contents;
}
?>

@file:p rofile.php
$variables = Array (' user ' = = $user);
Print theme_render_template (' user-profile.tpl.php ', $variables);
?>

http://www.bkjia.com/PHPjc/728097.html www.bkjia.com true http://www.bkjia.com/PHPjc/728097.html techarticle buffer buffer is a memory address space, the Linux system default size is generally 4096 (4KB), that is, a memory page. Mainly used for storage speed is not synchronized between the device or the priority of different devices ...

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