Home Backend Development PHP Tutorial Use PHP's ob_start() to control your browser cache_PHP tutorial

Use PHP's ob_start() to control your browser cache_PHP tutorial

Jul 21, 2016 pm 03:44 PM
cache header php start use function control Browser use of output

The output control function does not affect the file header information sent using header() or setcookie(), only the data blocks similar to echo() and PHP code.
Let’s take a simple example first to give everyone a general impression of Output Control:
Example 1.

Copy code The code is as follows :

ob_start(); //Open buffer
echo "Hellon"; //Output
header("location:index.php") ; //Redirect the browser to index.php
ob_end_flush(); //Output all content to the browser
?>

Everyone who knows the header() function Everyone knows that this function will send a file header to the browser, but if there is any output before using this function (including empty output, such as spaces, carriage returns, and line feeds), an error will be prompted. If we remove ob_start() in the first line and then execute this program, we will find that we get an error message: "Header had all ready send by"! But with ob_start, there will be no error message. The reason is that when the buffer is opened, the characters after echo will not be output to the browser, but will be retained on the server. They will not be output until you use flush or ob_end_flush, so it will not Any file header output errors!
1. Introduction to related functions:
1. Flush: refresh the contents of the buffer and output.
Function format: flush()
Description: This function is frequently used and is very efficient.
2. ob_start: Open the output buffer
Function format: void ob_start(void)
Description: When the buffer is activated, all non-file header information from the PHP program will not be sent, but saved. in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
3. ob_get_contents: Returns the contents of the internal buffer.
Usage: string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.
4. ob_get_length: Returns the length of the internal buffer.
Usage: int ob_get_length(void)
Description: This function will return the length in the current buffer; the same as ob_get_contents, if the output buffer is not activated. then returns FALSE.
5. ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer.
Usage: void ob_end_flush(void)
Description: This function sends the contents of the output buffer (if any).
6. ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
Usage method: void ob_end_clean(void)
Description: This function will not output the contents of the internal buffer but delete it !
7. ob_implicit_flush: Turn on or off absolute refresh
Usage method: void ob_implicit_flush ([int flag])
Note: Anyone who has used Perl knows the meaning of $|=x, this string can be turned on /Close the buffer, and the ob_implicit_flush function is the same as that. The default is to close the buffer. After turning on absolute output, each script output is sent directly to the browser, and there is no need to call flush()
2. In-depth understanding:
1. About the Flush function:
This function appeared in PHP3. It is a very efficient function. It has a very useful function of refreshing the browser's cache. Let's give a very obvious operation effect. Example to illustrate flush.
Example 2.
Copy code The code is as follows:

for($i = 1; $i <= 300; $i++ ) print(" ");
// This sentence is very critical. The structure of the cache makes its content available from the browser only when it reaches a certain size. Output inside
// In other words, if the cache content does not reach a certain size, it will not be output before the program execution is completed. After
// testing, I found that the bottom limit of this size is 256 characters long. This means that the content received by the cache in the future will
// be sent out continuously.
For($j = 1; $j <= 20; $j++) {
echo $j."
";
flush(); //This will add something to the cache The content is squeezed out and displayed on the browser
sleep(1); //Let the program "sleep" for a second, which will allow you to see the effect more clearly
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320445.htmlTechArticleThe output control function does not affect the file header information sent using header() or setcookie(), only those Data blocks similar to echo() and PHP code work. Let’s give a simple example first...
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What is the reason for redirecting 404 errors after logging in with Selenium? How to solve it? What is the reason for redirecting 404 errors after logging in with Selenium? How to solve it? Apr 01, 2025 pm 10:54 PM

Solution to Redirecting 404 Errors after Simulation Login When using Selenium for Simulation Login, we often encounter some difficult problems. �...

How to implement cross-application jump for Word plug-in login authorization? How to implement cross-application jump for Word plug-in login authorization? Apr 01, 2025 pm 11:27 PM

How to implement cross-application jump for Word plug-in login authorization? When using certain Word plugins, we often encounter this scenario: click on the login in the plugin...

Why did the small window become blank after scanning the QR code on WeChat? How to solve it? Why did the small window become blank after scanning the QR code on WeChat? How to solve it? Apr 01, 2025 pm 03:54 PM

Handle the problem of blank small window after scanning the QR code on WeChat. When using WeChat to scan the QR code on WeChat, we often encounter some unexpected problems. A common...

The Python subprocess module fails to execute the wmic datafile command. How to solve it? The Python subprocess module fails to execute the wmic datafile command. How to solve it? Apr 01, 2025 pm 08:48 PM

Use Python's subprocess module to execute wmic...

How to avoid third-party interfaces returning 403 errors in Node environment? How to avoid third-party interfaces returning 403 errors in Node environment? Apr 01, 2025 pm 02:03 PM

How to avoid the third-party interface returning 403 error in the Node environment. When calling the third-party website interface using Node.js, you sometimes encounter the problem of returning 403 error. �...

How to solve the problem of missing dynamic loading content when obtaining web page data? How to solve the problem of missing dynamic loading content when obtaining web page data? Apr 01, 2025 pm 11:24 PM

Problems and solutions encountered when using the requests library to crawl web page data. When using the requests library to obtain web page data, you sometimes encounter the...

See all articles