In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what are the PHP output cache ob series functions", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn what are the PHP output cache ob series functions!
The basic principle of ob: if ob caching is turned on, echo's data is first placed in the ob cache. If it is header information, put it directly in the program cache. When the page is executed at the end, the data cached by ob is put into the program cache and then returned to the browser in turn.
Let me talk about the basic functions of ob:
1) prevent errors caused by sending header files using functions such as setcookie (), header (), or session_start () after the browser has output. In fact, this usage is better to use less, develop good code habits.
2) capturing the output of some unavailable functions, such as phpinfo (), outputs a lot of HTML, but we can't capture it with a variable such as $info=phpinfo ();, so ob works.
3) process the output, such as gzip compression, such as simple and complex conversion, such as some string substitution.
4) to generate a static file is to capture the output of the whole page and save it as a file. It is often used in generating HTML or in full-page caching.
For the third point just mentioned in the GZIP compression, many people may want to use, but not really used, in fact, slightly modify the code, you can achieve the gzip compression of the page.
The copy code is as follows:
Ob_start (ob_gzhandler)
Content to be cached
Yes, it is fine to add an ob_gzhandler callback function, but there are some minor problems: one is that you need zlib support, and the other is that you have not judged whether the browser supports gzip (now it seems to support it, and iphone browsers all seem to support it).
The previous approach was to determine whether the browser supports gzip, then use a third-party gzip function to compress the contents of ob_get_contents (), and finally echo.
I. Collection of functions commonly used in ob series of functions
The copy code is as follows:
Ob_start (); / / opens an output buffer so that all output information is no longer sent directly to the browser, but is saved in the output buffer.
Ob_clean (); / / removes the contents of the internal buffer without closing the buffer (no output).
Ob_end_clean (); / / removes the contents of the internal buffer and closes the buffer (no output).
Ob_get_clean (); / / returns the contents of the internal buffer and closes the buffer. Equivalent to executing ob_get_contents () and ob_end_clean ()
Ob_flush (); / / sends the contents of the internal buffer to the browser, deletes the contents of the buffer, and does not close the buffer.
Ob_end_flush (); / / sends the contents of the internal buffer to the browser, deletes the contents of the buffer, and closes the buffer.
Ob_get_flush (); / / returns the contents of the internal buffer, closes the buffer, and then releases the contents of the buffer. Equivalent to ob_end_flush () and returns the contents of the buffer.
Flush (); / / outputs all the contents released by ob_flush and those that are not in the PHP buffer to the browser; flushes the contents of the internal buffer and outputs them.
Ob_get_contents (); / / returns the contents of the buffer and does not output.
Ob_get_length (); / / returns the length of the internal buffer, and this function returns FALSE if the buffer is not activated.
Ob_get_level (); / / Return the nesting level of the output buffering mechanism.
Ob_get_status (); / / Get status of output buffers.
Ob_implicit_flush (); / / turns on or off absolute refresh, which defaults to off. When turned 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 instead of calling flush () or waiting until the end of the script to output.
The ob_gzhandler / / ob_start callback function, which compresses the contents of the buffer with gzip.
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 setting:
When the value output_buffering / / is ON, output control is used in all scripts; if the value is a number, it represents the maximum byte limit of the buffer, and when the cache content reaches this upper limit, the contents of the current buffer are automatically output to the browser.
Output_handler / / this option redirects all the output of the script to a function. For example, when output_handler is set to mb_output_handler (), the encoding of the characters is modified to the specified encoding. Any handler set will automatically handle the output buffer.
Implicit_flush / / works like ob_implicit_flush, and defaults to Off.
II. Illustration with examples
1. Make the header () function preceded by echo code
The Output Control function gives you free control over the output of the 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 has no effect on the header information sent using header () or setcookie (), but only on blocks similar to echo () and PHP code.
The copy code is as follows:
Ob_start (); / / Open the buffer
Echo "Hello\ n"; / / output
Header ("location:index.php"); / / redirect the browser to index.php
Ob_end_flush (); / / output all content to browser
Anyone who knows anything about the header () function knows that this function sends a header to the browser, but if you have any output (including empty output, such as spaces, carriage returns, and line feeds) before using the function, an error will occur. If we remove the ob_start () in the first line and execute this program, we will find that we get an error message: "Header had all ready send by"! But add ob_start, there will be no error, because when the buffer is opened, the characters after echo will not be output to the browser, but will remain on the server until you use flush or ob_end_flush, so there will not be any header output error!
2. Save the output of the phpinfo () function
The copy code is as follows:
Ob_start (); / / Open the buffer
Phpinfo (); / / use the phpinfo function
$info = ob_get_contents (); / / get the contents of the buffer and assign a value to $info
$file = fopen ('info.txt', 'w'); / / Open the file info.txt
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 make users get the html pages generated by PHP on the client side in some way. If the html page will not be updated, then when another user visits the page again, the program will no longer call PHP and related databases, for some informative sites, such as sina, 163, sohu. The benefits of technology like this are enormous.
The copy code is as follows:
Ob_start (); / / Open the buffer
Full output of the php page
$content = ob_get_contents (); / / get all the output of the php page
$fp = fopen ("output00001.html", "w"); / / create a file and open it, ready to write
Fwrite ($fp, $content); / / write all the contents of the php page to output00001.html, and then.
Fclose ($fp)
3. Output cache handle ob_gzhandler
PHP4.0.4 has a new output cache handle, ob_gzhandler, which is similar to the previous class, but used differently. When using ob_gzhandler, you want to add the following to the php.ini:
The copy code is as follows:
Output_handler = ob_gzhandler
This line of code causes PHP to activate the output cache and compress everything it sends out.
If for some reason you don't want to add this line of code to php.ini, you can also change the default server behavior (not compressed) through the .htaccess file in the directory where the PHP source file is located. The syntax is as follows:
The copy code is as follows:
Php_value output_handler ob_gzhandler
Or it can be called from PHP code, as follows:
The copy code is as follows:
Ob_start ("ob_gzhandler")
The method of using output cache handles is indeed very effective and does not impose any special load on the server. It is important to note, however, that Netscape Communicator does not support compressed graphics very well, so unless you can guarantee that all users will use IE browsers, you should prohibit compressing JPEG and GIF graphics. In general, this compression works for all other files, but it is recommended that you test it separately for various browsers, especially if you are using a special plug-in or data viewer.
Note:
1. The output_buffering of some Web servers defaults to 4069 characters or greater, that is, the output content must reach 4069 characters before the server flush refreshes the output buffer. To ensure that the flush is valid, it is best to have the following statement before the ob_flush () function:
The copy code is as follows:
Print str_repeat ("", 4096); / / to ensure that the output_ buffering value is reached
2. Ob_* series functions operate on the output buffer of PHP itself, so ob_flush only flushes the buffer of PHP itself, while flush is the buffer of flushing apache. Therefore, the order in which the two are used correctly is: first ob_flush, then flush. Ob_flush releases data from the PHP buffer, while flush sends all data inside and outside the buffer to the browser.
3. Don't mistakenly think that after using ob_start (), the output such as echo/print of the script will never be displayed on the browser. Because when the PHP script finishes running, the buffer is automatically flushed and the contents are output.
At this point, I believe that everyone on the "PHP output cache ob series of functions have a deeper understanding, might as well to the actual operation of it!" Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.