Home > Backend Development > PHP Tutorial > PHP outputs messages to the browser in real time_PHP tutorial

PHP outputs messages to the browser in real time_PHP tutorial

WBOY
Release: 2016-07-13 17:48:33
Original
1419 people have browsed it

One of Yahoo's front-end optimization practices is to output the part of the html first (Flush the Buffer Early). In this way, after the browser obtains the head, it can download the css/js file in the head first without waiting for the entire After the html is downloaded, download the css/js in the head, thereby improving the speed of opening the web page.

A Transfer-Encoding: chunked header has been added to http1.1. The function of this header can be to divide the message into multiple chunks for output.

The format of the message is as follows:
Java code
Chunked-Body = *chunk
"0" CRLF
    footer
    CRLF
chunk = chunk-size [ chunk-ext ] CRLF
   chunk-data CRLF
hex-no-zero =
chunk-size = hex-no-zero *HEX
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-value ] )
chunk-ext-name = token
chunk-ext-val = token | quoted-string
chunk-data = chunk-size(OCTET)
footer = *entity-header

Chunked-Body = *chunk
    "0" CRLF
    footer
    CRLF
​chunk = chunk-size [ chunk-ext ] CRLF
   chunk-data CRLF
hex-no-zero =
​chunk-size = hex-no-zero *HEX
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-value ] )
chunk-ext-name = token
chunk-ext-val = token | quoted-string
​chunk-data = chunk-size(OCTET)
footer = *entity-header
CRLE: Carriage return and line feed (rn)
For example:
Java code
...​​​​​​​​​​​​ Transfer-Encoding: chunked #Start the message after 2 CRLEs in the header
                                                                                           1 The size of #chunk in hexadecimal, then add CRLE
a​​​​​​​​ 4 The size of #chunk in hexadecimal, then add CRLE
test​​​​​​​ 0                                  #chunk ends, 0 + 2 CRLEs

...                                                                                 Transfer-Encoding: chunked #Start the message after 2 CRLE headers
                                                                                     1 The size of #chunk in hexadecimal, then add CRLE
a​​​​​​​​ 4 The size of #chunk in hexadecimal, then add CRLE
test​​​​​​​0​​​​​​​​​


After using ob_flush in PHP, the Transfer-Encoding: chunked header will be automatically added to achieve chunked output, but the effect is often not achieved during use. I have to consider some issues

1. PHP buffer
If your php is running with the apache module, please use the flush function to notify php output. If running in fastcgi mode, use ob_flush to notify php. At this time, gzip will fail, and the Chunked method does not support independent compression of each block. You can only output the compressed package in its entirety and then output the compressed package in chunks. To ensure compatibility, call ob_flush first and then flush.

2. Browser buffer
How the browser reacts when encountering the Transfer-Encoding: chunked header depends on the browser's implementation. In my experiment, Firefox will display the chunk data in real time regardless of the size of the chunk, while IE8 and Chrome will display it after a certain length. Therefore, it is necessary to output a certain size first before some browsers can be effective.

3. Reverse proxy server
Does the reverse proxy server you use support the http1.1 protocol? How does it handle the backend being chunked? Will it be output according to the backend if it encounters chunked before the proxy buffer is full?

The proxy function of nginx only supports http1.0, and it will only output when the proxy buffer is full.

4. FastCGI Buffer
If running in FastCGI mode, the Web Server may have its own fastcgi buffer and wait until the buffer is full before outputting (nginx does this). The flush function can only notify PHP's output buffer output

Chunked transfer encoding
Hypertext Transfer Protocol -- HTTP/1.1 Chunked transfer encoding
Understand the difference between ob_flush and flush

http://www.bkjia.com/PHPjc/478424.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478424.htmlTechArticleOne of Yahoo’s front-end optimization practices is to output the head part of the html first (Flush the Buffer Early) , in this way, the browser can download the css/js file in the head first after getting the head, without waiting...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template