Introduction to using php buffer output_buffering and ob_start

Source: Internet
Author: User
Tags drupal
This article mainly introduces the php buffer output_buffering and ob_start related information. For more information, see Buffer

Buffer is a memory address space. the default size of a Linux system is generally 4096 (4 KB), that is, a memory page. It is mainly used for data transmission between devices with different storage speeds or devices with different priorities. Through the buffer, the mutual waits of processes can be reduced. Here is a simple example. when you open a text editor and edit a file, the operating system does not immediately write this character to the disk for each input, instead, it is written to the buffer first. when the buffer is full, the data in the buffer is written to the disk. of course, when the kernel function flush () is called, the dirty data in the buffer must be written back to the disk.
In the same way, when echo and print are executed, the output is not immediately transmitted to the client browser through tcp, but is written to the php buffer. The php output_buffering mechanism means that a new queue is established before the tcp buffer, and data must pass through the queue. When a php buffer is full, the script process sends the output data in the php buffer to the system kernel and sends it to the browser for display. Therefore, the data will be written to these places in sequence echo/pring-> php buffer-> tcp buffer-> browser

Php output_buffering

By default, php buffer is enabled, and the default value of this buffer is 4096, that is, 4 KB. You can use. in the ini configuration file, find output_buffering configuration. when echo, print, and other user data are output, the output data will be written to php output_buffering until output_buffering is fully written. The data will be transmitted to the browser over tcp for display. You can also manually activate php output_buffering through ob_start (), so that even if the output exceeds 4 kB, the data is not really sent to the browser over tcp, because ob_start () set the php buffer space to large enough. Data is sent to the client browser only after the script is completed or the ob_end_flush function is called.
1. when output_buffering = 4096, and a small amount of data is output (less than one buffer)

The code is as follows:


For ($ I = 0; $ I <10; $ I ++ ){
Echo $ I .'
';
Sleep ($ I + 1 );//
}
?>

Symptom: no intermittent output occurs every few seconds, but the output can be viewed at a time until the response ends. the browser interface remains blank until the server script processing ends. This is because the data volume is too small, and php output_buffering is not full. Data writing order, which is echo-> php buffer-> tcp buffer-> browser
2. when output_buffering = 0, and a small amount of data is output (less than one buffer)

The code is as follows:


// Ini_set ('output _ buffering ', 0) does not take effect
// Edit/etc/php. ini and set output_buffering = 0 to disable the output buffering mechanism.
// Ini_set ('output _ buffering ', 0); // completely disable the output buffering function
For ($ I = 0; $ I <10; $ I ++ ){
Echo $ I .'
';
Flush (); // notifies the underlying operating system to send data to the client browser as soon as possible
Sleep ($ I + 1 );//
}
?>

Symptom: it is inconsistent with the display just now. after the php buffering mechanism is disabled, intermittent output can be seen intermittently in the browser, and the output does not have to be seen until the script is executed. This is because the data is not stuck in php output_buffering. The data write order is echo-> tcp buffer-> browser.
3. when output_buffering = 4096. the output data is greater than a buffer and ob_start () is not called ()
Create a 4 kB file
$ Dd if =/dev/zero of = f4096 bs = 4096 count = 1

The code is as follows:


For ($ I = 0; $ I <10; $ I ++ ){
Echo file_get_contents ('./f4096'). $ I .'
';
Sleep ($ I + 1 );
}
?>

Symptom: the response is not over yet (the http connection is not closed). intermittent output is displayed intermittently, and the browser interface is not left blank. Although the php output_buffering mechanism is enabled, the output will still be intermittent, rather than one-time, because the output_buffering space is insufficient. Every time a php buffering is filled, the data is sent to the client browser. 4. when output_buffering = 4096, the output data is greater than a tcp buffer and ob_start () is called ()

The code is as follows:


Ob_start (); // enable php buffer
For ($ I = 0; $ I <10; $ I ++ ){
Echo file_get_contents ('./f4096'). $ I .'
';
Sleep ($ I + 1 );
}
Ob_end_flush ();
?>

Symptom: The complete output is not displayed until the script processing on the server end and the response is complete. the output interval is very short, so that you cannot feel the pause. Before the output, the browser maintains a blank page, waiting for the server data. This is because, once php calls the ob_start () function, it will extend the php buffer to a large enough value, the data in the php buffer is sent to the client browser only after the ob_end_flush function call or the completion speed of script running.
Tcpdump observation
Here, we use tcpdump to Monitor tcp packets and observe the difference between using ob_start () and not using it.
1. ob_start () is not used ()
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 7301: 8412 (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 15712: 16624 (912) 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 23924: 24836 (912) 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 32136: 33048 (912) 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 40348: 41260 (912) 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 6432 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. 514361 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port :. ack 486 win 6432
2. ob_start () is used ()
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 33581: 35041 (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 40881: 41233 (352) ack 485 win 6432 12:36:51. 570364 IP 192.168.0.8.webcache> 192.168.0.28.noagent :. ack 486 win 6432
Through the comparison above, we can see that the time interval of data packets is significantly different. If ob_start () is not used, the time interval is large. wait about 4 seconds to send the data in tcp buffer. If the data remains in the php buffer for a long time, the output data is sent to the client browser. This is because the php buffer will soon be fully written and the data has to be sent out. When ob_start () is enabled, data packets are sent to the client at almost the same time. This can be inferred that the data stays in the php buffer until ob_end_flush () is called to send the data in the php buffer to the client browser.

Output buffering function

1. ob_start

Activate the output_buffering mechanism. Once activated, the script output is not directly sent to the browser, but is temporarily written to the php buffer memory area.
By default, php enables the output_buffering mechanism. However, by calling the ob_start () function, the value of output_buffering is extended to a large enough value. You can also specify $ chunk_size to specify the value of output_buffering. $ Chunk_size: The default value is 0, indicating that data in php buffer will not be sent to the browser until the script stops running. If you set the size of $ chunk_size, it indicates that as long as the data length in the buffer reaches this value, the data in the buffer will be sent to the browser.
Of course, you can specify $ ouput_callback to process data in the buffer. For example, the ob_gzhandler function compresses the data in the buffer and then transmits it to the browser.

2. ob_get_contents

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

3. ob_end_flush and ob_end_clean

These two functions are similar, and the ouptu_buffering mechanism is disabled. But the difference is that ob_end_flush only flush the data in the php buffer to the client browser, while ob_clean_clean clears the data in the php bufeer (erase), but does not send it to the client browser. After ob_end_flush is called, data in php buffer still exists, and ob_get_contents () can still obtain data copies in php buffer. After ob_end_clean () is called, ob_get_contents () gets an empty string, and the browser cannot receive the output, that is, no output.

Typical cases

Ob_start () is often used in some template engines and page file caches. They can be found in well-known open-source projects such as wordpress, drupal, and smarty. Here, the drupal application is extracted.
Template File

The code is 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: profile. php
$ Variables = array ('user' => $ user );
Print theme_render_template ('User-profile. tpl. php', $ variables );
?>

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.