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

Jul 13, 2016 pm 05:48 PM
head html php yahoo optimization front end real time practice yes Browser of output

One of Yahoo's front-end optimization practices is to output the <head> 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 = <HEX excluding "0">
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 = <HEX excluding "0">
​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...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

Sesame Open Door Official Website Sesame Open Door App Latest Entering Website Sesame Open Door Official Website Sesame Open Door App Latest Entering Website Feb 28, 2025 am 11:18 AM

Sesame Open Door Official Website Sesame Open Door App Latest Entering Website

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

The latest version of Sesame Open Door Official Website Entrance gate.io Official Website Link Address Entrance The latest version of Sesame Open Door Official Website Entrance gate.io Official Website Link Address Entrance Feb 28, 2025 am 11:21 AM

The latest version of Sesame Open Door Official Website Entrance gate.io Official Website Link Address Entrance

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

PHP Program to Count Vowels in a String

gate.io sesame door download Chinese tutorial gate.io sesame door download Chinese tutorial Feb 28, 2025 am 10:54 AM

gate.io sesame door download Chinese tutorial

Sesame Open Door Exchange official website gate.io exchange official website entrance Sesame Open Door Exchange official website gate.io exchange official website entrance Feb 28, 2025 am 10:45 AM

Sesame Open Door Exchange official website gate.io exchange official website entrance

See all articles