


Detailed explanation of PHP output cache ob series functions_PHP tutorial
Basic principle of ob: If the ob cache is turned on, the echo data is first placed in the ob cache. If it is header information, it is placed directly in the program cache. When the page is executed to the end, the ob cached data will be placed in the program cache, and then returned to the browser in turn.
Let me talk about the basic functions of ob:
1) Prevent the use of functions such as setcookie(), header() or session_start() to send header files after the browser has output. error. In fact, it is better to use this kind of usage less often and develop good coding habits.
2) Capture the output of some unobtainable functions. For example, phpinfo() will output a lot of HTML, but we cannot use a variable such as $info=phpinfo(); to capture it. At this time, ob will be useful. .
3) Process the output content, such as gzip compression, conversion between Simplified and Traditional Chinese, and some string replacement.
4) Generating static files is actually capturing the output of the entire page and then saving it as a file. Often used in HTML generation or full page caching.
Regarding the GZIP compression mentioned in the third point just mentioned, many people may want to use it, but have not actually used it. In fact, by slightly modifying the code, you can achieve gzip compression of the page.
Content to be cached
Yes, Just add a callback function called ob_gzhandler, but there are some minor problems with this. First, it requires zlib support, and second, it does not determine whether the browser supports gzip (it seems to support it now, and all iPhone browsers seem to support it).
The previous approach was to determine whether the browser supports gzip, then use the third-party gzip function to compress the content of ob_get_contents(), and finally echo.
1. Collection of commonly used functions in ob series functions
ob_start(); //Open an output buffer. All output information is no longer sent directly to the browser, but is saved in the output buffer.
ob_clean(); //Delete the contents of the internal buffer without closing the buffer (no output).
ob_end_clean(); //Delete the contents of the internal buffer and close the buffer (no output).
ob_get_clean(); //Return the contents of the internal buffer and close the buffer. Equivalent to executing ob_get_contents() and ob_end_clean()
ob_flush(); sends the content of the buffer and deletes the content of the buffer without closing the buffer.
ob_end_flush(); //Send the contents of the internal buffer to the browser, delete the contents of the buffer, and close the buffer.
ob_get_flush(); //Return the contents of the internal buffer, close the buffer, and then release the contents of the buffer. Equivalent to ob_end_flush() and returns the buffer contents.
flush(); // Output the content released by ob_flush and the content not in the PHP buffer to the browser; refresh the content of the internal buffer and output it.
ob_get_contents(); //Return the contents of the buffer without output.
ob_get_length(); //Returns the length of the internal buffer. If the buffer is not activated, this function returns FALSE.
ob_get_level(); //Return the nesting level of the output buffering mechanism.
ob_get_status(); //Get status of output buffers.
ob_implicit_flush(); //Turn on or off absolute refresh. The default is off. After turning on ob_implicit_flush(true), the so-called absolute refresh means that when an output statement (e.g: echo) is executed, the output is sent directly to The browser no longer needs to call flush() or wait until the end of the script to output.
ob_gzhandler //ob_start callback function, uses gzip to compress the contents of the buffer.
ob_list_handlers //List all output handlers in use
output_add_rewrite_var //Add URL rewriter values
output_reset_rewrite_vars //Reset URL rewriter values
The behavior of these functions is affected by the php_ini settings:
output_buffering //When this value is ON, output control will be used in all scripts; if the value is a number, it represents the maximum byte limit of the buffer , when the cached content reaches the upper limit, the content in the current buffer will be automatically output to the browser.
output_handler //This option can redirect all output of the script to a function. For example, when output_handler is set to mb_output_handler(), the character's encoding will be modified to the specified encoding. Any processing functions set will automatically handle output buffering.
implicit_flush //The function is the same as ob_implicit_flush, the default is Off.
2. Examples
1. There can be echo code before the header() function
The Output Control function allows you to freely control the output of data in the script. It is very useful, especially when you want to output the file header after the data has been output.
The 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.
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 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 break) will prompt an error. 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!
2. Save the output of the phpinfo() function
fwrite($file, $info); //Write information to info.txt
fclose($file); //Close the file info.txt
3. Static template technology
The so-called static template technology is to use a certain method to enable users to get the html page generated by PHP on the client side. If this HTML page will no longer be updated, then when another user browses this page again, the program will no longer call PHP and related databases. For some websites with a large amount of information, such as sina, 163, and sohu. The benefits of technology like this are huge.
Copy code
3. Output cache handle ob_gzhandler
PHP4.0.4 has a new output cache handler ob_gzhandler, which is similar to the previous class, but its usage is different. The content to be added to php.ini when using ob_gzhandler is as follows:
Copy code
The code is as follows:
Copy code
The code is as follows:
Copy code
The code is as follows:
The method of using the output cache handle is indeed very effective and does not bring any special load to the server. But it must be noted that Netscape Communicator has poor support for compressed graphics, so unless you can ensure that all users use IE browser, you should disable compressed JPEG and GIF graphics. In general, this compression works for all other files, but it is recommended that you test it separately for each browser, especially if you use special plug-ins or data viewers. This is especially important.
Notes:
1. The output_buffering of some web servers defaults to 4069 characters or larger, that is, the output content must reach 4069 characters before the server flushes the output buffer. In order to ensure that the flush is effective, It is best to have the following statement before the ob_flush() function:
2. The ob_* series of functions operate the output buffer of PHP itself, so ob_flush only refreshes PHP's own buffer, while flush refreshes the apache buffer. Therefore, the correct order to use the two is: ob_flush first, then flush. ob_flush releases data from PHP's buffer, and flush sends all data in/out of the buffer to the browser.
3. Don’t mistakenly think that after using ob_start(), the script’s echo/print and other output will never be displayed in the browser. Because after the PHP script ends, the buffer will be automatically refreshed and the content will be output.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
