PHP--->php buffer principle

Source: Internet
Author: User
Tags php script

PHP buffer principle 1. Buffering process
    • echo from PHP script (print, print_r ...) After the content, how to display to the user, see the process below
echo、print => php output_buffering => webServer buffer => browser buff => browser display//即:脚本输出 => php的缓冲区设置 => 系统的缓冲区设置(apache、nginx) => 浏览器的缓冲区设置 => 显示给用户
2. PHP Buffer
- php运行的结果先放入缓冲区(buffer),只有当缓冲区满了或者php运行完毕,才将数据输出去。- 缓冲区是通过php.ini中的output_buffering变量控制,可以设置大于0的数值来打开buffer。- ob_start()手动激活php output_buffering机制,使得即便输出超过了4kb数据,也不把数据交给tcp传给浏览器,因为ob_start()将php buffer空间设置到了足够大 。只有直到脚本结束,或者调用ob_end_flush函数,才会把数据发送给客户端浏览器。需要注意的是php.ini中php buffer是关闭的,再次调用ob_end_flush()会报warning。
3.webServer Buffer
  • This is mainly about Apache and Nginx buffers.
    • 1. Apache Buffer
      • When the PHP output data to the Apache server, it will also do a layer of buffer (also put the data into its buffer, when the buffer data is full or completed, the output data).
      • If you want to close the buffer, you can use Flush () at the PHP layer to force the buffer data output.
      • Fulsh () works: in Apache module SAPI, flush will call Sapi_module's flush member function pointer, indirect call Apache Api:ap_rflush refresh Apache output buffer, Of course the manual also says that there are some other Apache modules that may change the results of this action. For example, Mod_gzip may make its own output buffer, which causes the flush () function to produce results that are not immediately sent to the client browser.
    • 2, Nginx Buffer
      • Nginx uses the fastcgi buffer to buffer the data. Unfortunately, the fastcgi is forced to open buffer and cannot be closed.
      • Someone might think that can't be turned off you can set the buffer to be small enough to allow the buffered data output to achieve a unbuffered effect. But the idea couldn't be realized.
        Reason one: fastcgi buffer does not recognize values less than 1k.
        Reason two: Affected by the size relationship between parameters.
      • Specifically, you can see some of the buffer settings for fastcgi.
        Fastcgi_buffer_size: The header data used to store the response.
        Fastcgi_buffers: The content data used to store response.
        Fastcgi_busy_buffers_size: Used to control the number of buffer transfers to the client at the same time. Once the Fastcgi_buffers set buffer is written, until the data in buffer is completely transmitted (transmitted to the client), the buffer will remain in the busy state and we cannot do anything else with these buffer. All buffer size in the busy state cannot add up to more than fastcgi_busy_buffers_size.
      • Size Relationship between parameters:
        Fastcgi_busy_buffers_size < (all fastcgi_buffers–one buffer) and Fastcgi_busy_buffers_size>=max (Fastcgi_buffer_ Size, one fastcgi _buffers).
        For example, in the nginx.conf configuration, there are:
        Fastcgi_buffers 4 128k
        Fastcgi_buffer_size 256k
        So fastcgi_busy_buffers_size< (4*128k–4k) and Fastcgi_busy_buffers_size>=max (256k, 128k)
        Where 4k (the size of one buffer) is the default cache size of the Linux system, which is a memory page.
        If the fastcgi_buffer_size is set very small, it will cause the header too small error. You also cannot guarantee that the set value will satisfy all situations.
      • It is important to note that:
        Flush, strictly speaking, this only works when PHP is installed as an Apache module (handler or filter). It is a buffer that refreshes the webserver (which can be considered specifically Apache). So the flush () function is not working under Nginx.
4.browser Buffer
    • IE 256Bytes, Chrome and Firefox 1000Bytes, only the output data reached this length or script end browser will be output data on the page.
    • Unable to close browser buffer on PHP side.
      In order to make the data output in time, you can send some space to fill the buffer of the browser before sending the real content data.
      When the browser's buffer is full, the other new output data is output.
      However, different browsers will set different buffer sizes.
      For the duration of the insurance, 4,096 spaces can be sent because the currently popular browser buffer is not more than 4k (an inside page size).
5. Output buffer related functions are:

Ob_start ()-Open output control buffer
Ob_get_length ()-Returns the length of the output buffer
Ob_get_level ()-Returns the nesting level of the output buffer
Ob_get_status ()-Returns the state of the output buffer (returned as an array, returning the topmost layer by default, all when the parameter is true)
Ob_get_contents ()-Returns the contents of the output buffer
Ob_get_clean ()-Returns the current output buffer in string format and turns off output buffering
Ob_end_clean ()-Empty (erase) buffer and turn off output buffering
Ob_get_flush ()-Returns the output buffer content as a string and closes the buffer
Ob_end_flush ()-Flushes out (sends out) the output buffer content buffer and turns off the output buffer

6. Discrimination
    • Ob_end_flush () differs from Ob_end_clean ()
      • Both functions will turn off the output buffer.
      • The difference is that Ob_end_flush () simply sends the data in the PHP buffer to the client browser, while Ob_clean_clean () deletes the data in the PHP buffer but does not send it to the client. After the Ob_end_flush () call, the data in the PHP buffer still exists, and ob_get_contents () can still get copies of the data in the PHP buffer.
    • Flush () and Ob_flush ()
      • Reference: https://www.cnblogs.com/phpper/p/7750104.html
      • The use of these two functions is even the most puzzling problem of many people, the manual on the interpretation of two functions are also vague, do not explicitly point out their differences, it seems that both of the function is to refresh the output cache. But in the code at the beginning of our article, if Fush () is replaced with Ob_flush (), the program will no longer execute correctly. Obviously, they are different, or else the manual directly indicates that one of the other functions is an alias, there is no need to explain separately. So what is the difference between them?
      • Repeated study of the manual, referring to some of the manuals in the message, I think it should be this: when the cache is not turned on, the content of the script output in the server side is waiting for the output state, flush () can wait for the output of the content immediately sent to the client. 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.

PHP--->php buffer principle

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.