PHP real-time output message to browser _php tutorial

Source: Internet
Author: User
One of Yahoo's front-end optimization practices is to first put the HTMLPart of the first output (Flush the Buffer Early), so that the browser can to download the head of the Css/js file, rather than wait until the entire HTML download to download the head of the CSS/JS, thereby increasing the speed of the page open.

A transfer-encoding:chunked header is added to the http1.1 to divide the message into multiple outputs.

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 line (\ r \ n)
For example:
Java code
... #很多报头
Transfer-encoding:chunked #报头2个CRLE后开始报文

1 #chunk的大小, Hex, then add a Crle
A #chunk数据, then add a Crle.
4 #chunk的大小, Hex, then add a Crle
Test #chunk数据, you can continuously loop the chunked output and add a Crle
0 #chunk结束, 0 + 2 x Crle

... #很多报头
Transfer-encoding:chunked #报头2个CRLE后开始报文

1 #chunk的大小, Hex, then add a Crle
A #chunk数据, then add a Crle.
4 #chunk的大小, Hex, then add a Crle
Test #chunk数据, you can continuously loop the chunked output and add a Crle
0 #chunk结束, 0 + 2 x Crle



After using Ob_flush in PHP, the transfer-encoding:chunked header is automatically added to the block output, but it often does not work in the process. Have to consider some questions

One, the buffer of PHP
If your PHP is running in an Apache module, use the Flush function to notify the PHP output. If you are running in fastcgi mode, use Ob_flush to notify PHP. At this time, Gzip will expire, chunked mode does not support the independent compression of each block. When only the output is compressed, the packet is compressed and the output is chunked. To ensure compatibility, call Ob_flush First, and then flush.

Second, the browser buffer
After encountering the transfer-encoding:chunked header, what the browser does, this still depends on the implementation of the browser. In my experiment, Firefox does show real-time regardless of chunk data size, while IE8 and chrome require a certain length to display. Therefore, some browsers have the effect of having to output a certain size first.

Third, reverse proxy server
Do you use the reverse proxy server to support the http1.1 protocol? How does it deal with the backend is the chunked way? Proxy buffer is not full before the chunked will follow the back end to output it?

The Nginx proxy function only supports http1.0, and it only outputs when proxy buffer is full.

Four, fastcgi buffer
If running in fastcgi mode, the Web server may have its own fastcgi buffer, waiting for the buffer to be full before output (Nginx). The flush function can only notify PHP output buffered outputs

Chunked transfer encoding
Hypertext Transfer Protocol--http/1.1 Chunked Transfer encoding
Deep understanding of the difference between Ob_flush and flush

http://www.bkjia.com/PHPjc/478424.html www.bkjia.com true http://www.bkjia.com/PHPjc/478424.html techarticle one of Yahoo's front-end optimization practices is to first export the head part of the HTML (Flush the Buffer Early), so that the browser can to download the head of the Css/js file, and not wait ...

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