PHP outputs messages to the browser in real time

Source: Internet
Author: User
One of Yahoo's frontend optimization practices is to first output the & lt; head & gt; part of html (FlushtheBufferEarly ), after the browser obtains the head, it can download the css/js file in the head first, instead of waiting until the entire html download is complete to download the css/js file in the head, thus improving the network... one of the frontend optimization practices of S Yahoo is to putPartial first output (Flush the Buffer Early). after the browser obtains the head, it can first download the css/js file in the head, you do not need to wait until the entire html file is downloaded to download the css/js file in the head, which increases the speed of web page opening.
 
A Transfer-Encoding: chunked header is added to http1.1. this header can be used to divide packets into multiple outputs.
 
The message format 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 feed (\ r \ n)
For example:
Java code
... # Many headers
Transfer-Encoding: chunked # start packet after two CRLE headers

1 # chunk size, hexadecimal, and then add a CRLE
A # chunk data, and then add a CRLE
4 # chunk size, hexadecimal format, and then add a CRLE
Test # chunk data, which can be repeatedly output in blocks and then added with a CRLE
0 # chunk ended, 0 + 2 CRLE
 
... # Many headers
Transfer-Encoding: chunked # start packet after two CRLE headers

1 # chunk size, hexadecimal, and then add a CRLE
A # chunk data, and then add a CRLE
4 # chunk size, hexadecimal format, and then add a CRLE
Test # chunk data, which can be repeatedly output in blocks and then added with a CRLE
0 # chunk ended, 0 + 2 CRLE
 
 
 
After ob_flush is used in php, the Transfer-Encoding: chunked header is automatically added to implement multipart output, but the effect is often not achieved during use. I have to consider some issues
 
I. php buffer zone
If your php runs in the apache module, use the flush function to notify php output. If you are running in fastcgi mode, use ob_flush to notify php. In this case, gzip will be invalid. Chunked mode does not support independent compression of each part. You can only compress all output files and output them in multiple parts. To ensure compatibility, call ob_flush and then flush.
 
II. browser buffer
When the browser responds to the Transfer-Encoding: chunked header, it depends on the implementation of the browser. In my experiment, firefox will be displayed in real time regardless of the chunk data size, while ie8 and chrome will be displayed after a certain length. Therefore, some browsers need to output a certain size before it works.
 
III. reverse proxy server
Does your reverse proxy server support http1.1? How does it handle chunked on the backend? Is chunked output based on backend when proxy buffer is not full?
 
Nginx's proxy function only supports http1.0, and it outputs only when the proxy buffer is full.
 
IV. FastCGI buffer
If you run in FastCGI mode, the Web Server may have its own fastcgi buffer, which is output only when the buffer is full ). The flush function can only notify php output buffer output.

Chunked transfer encoding
Hypertext Transfer Protocol -- HTTP/1.1 Chunked transfer encoding
Measure the test taker's understanding about the differences between ob_flush and flush.

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.