PHP output Cache OB series functions detailed

Source: Internet
Author: User
Tags file info flush html page ini php and php code php script phpinfo

 ob, output buffer, is the abbreviation for output buffering, not output cache. OB with the right, is able to have some help on speed, but blindly add the OB function, will only increase the extra burden of the CPU

The basic principle of OB: If the OB cache is turned on, the Echo data is first placed in the OB cache. If header information is placed directly in the program cache. When the page executes to the end, the OB-cached data is put into the program cache and then returned to the browser in turn. Let me say the basic role of OB:   1 prevents errors caused by functions that use Setcookie (), header (), or session_start () to send header files after the browser has output. In fact, this use less for good, develop good code habits.   2 captures the output of some unreachable functions, such as phpinfo (), which can output a large stack of HTML, but we can't use a variable such as $info=phpinfo () to capture, and then OB works.   3 Processing of output content, such as gzip compression, for example, for simple conversion, such as some string substitution.   4 to generate a static file, in fact, is to capture the entire page output, and then save as a file. Often used in generating HTML, or in full-page caching.   for just said the 3rd in the gzip compression, may be a lot of people want to use, but there is no real use, in fact, slightly modified the code, you can achieve the page's gzip compression.     Copy code code as follows: Ob_start (Ob_gzhandler); What to cache Yes, add a Ob_gzhandler this callback function, but there are some minor problems, one is the need for zlib support, the second is not to determine whether the browser support gzip (now seems to support, the iphone browser seems to support). The previous approach was to determine if the browser supports gzip and then compress the contents of ob_get_contents () with a third party gzip function, and finally echo.   One, OB series functions commonly used in the collection of functions     Copy Code code is as follows: Ob_start ();            //opens an output buffer, and all output information is not sent directly to the browser, but is stored in the output buffer.   Ob_clean ();            //deletes the contents of the internal buffer without closing the buffer (not output). Ob_end_clean ();        //deletes the contents of the internal buffer and closes the buffer (not output). Ob_get_cleAn ();        //returns the contents of the internal buffer and closes the buffer. Equivalent to the execution of 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 and closes the buffer, releasing the contents of the buffer. Corresponds to Ob_end_flush () and returns the contents of the buffer. Flush ();              //release ob_flush content, and not content in PHP buffer, output to browser, refresh the contents of internal buffer and output.   Ob_get_contents ();    //Returns the contents of the buffer and does not output. Ob_get_length ();      //Returns the length of the internal buffer, which 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 ();  //Turn on or off absolute refresh, default to OFF, open Ob_implicit_flush (true), the so-called absolute refresh, that is, when there is output statement (E.g:echo) is executed, the output is sent directly to the browser, and no longer need to call the flush () Or wait until the end of the script to output.   Ob_gzhandler              //Ob_start callback function that compresses the contents of the buffer with gzip. Ob_list_handlers          //list all output handlers to 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 the value is on, output control is used in all scripts, and if the value is a number, the maximum byte limit for the buffer is represented, and the contents of the current buffer are automatically exported to the browser when the cache content reaches that limit. Output_handler        //This option redirects all output of the script to a function. For example, when the Output_handler is set to Mb_output_handler (), the encoding of the character will be modified to the specified encoding. Set any processing function that will automatically process the output buffer. Implicit_flush        //function with Ob_implicit_flush, defaults to OFF.       II, example to explain   1, the header () function can have the Echo Code output control function allows you to free the output of the data in the script. It's very useful, especially for: when you want to output the file header after the data has been exported. Output control functions do not use header () or Setcookie (), send header information to affect, only for those similar to echo () and PHP code blocks of data have effect.       Copy code code as follows: Ob_start ();                  //Open buffer   echo "Hellon";             &NBSP Output   Header ("location:index.php"); redirect browser to index.php    Ob_end_flush ();              //output all content to the browser all people who know about the header () function know that this function sends a file header to the browser. However, if you have any output before using this function (including null output, such as a space, carriage return, and linefeed), you will be prompted for an error. If we remove the first line of Ob_start () and then execute the program, we will find an error message: "Header had all ready send by"! But with Ob_start, you won't be prompted for an error, because when the buffer is open, the characters behind Echo are not exported to the browser, but remain on the server until you use flush or ob_end_flush to output, so there is no error in the header output! 2, Save the Phpinfo () function output copy code code as follows: Ob_start ();                      //Open buffer    phpinfo ();                      //Use phpinfo function    $info = Ob_get_con Tents ();      //Get buffer area content and assign value to $info    $file = fopen (' Info.txt ', ' W ');  //Open File Info.txt    fwrite ($file, $info);            //write information to Info.txt    fclose ($file);                  //close fileInfo.txt 3, static template technology The so-called static template technology is in some way, so that users get the client side is generated by PHP HTML page. If the HTML page is not updated, then when another user browses the page again, the program will no longer invoke PHP and the associated database, for some of the more informative sites, such as Sina, 163, Sohu. The benefits of this kind of technology are enormous.   Copy code code as follows: Ob_start ();                            //Open buffer php page all output     $content = ob_get_contents ();          //get all content of PHP page output    $fp = fopen ("output00001.html", "w");  //Create a file and open, ready to write    fwrite ($fp, $content);                //write the contents of PHP page to output00001.html, then ...    fclose ($FP); The output cache handle Ob_gzhandler PHP4.0.4 has a new output-cache handle Ob_gzhandler, which is similar to the previous class but differs in usage. The content to be added in php.ini when using Ob_gzhandler is as follows: The    code is as follows: Output_handler = Ob_gzhandler; This line of code enables PHP to activate the output cache and compress all the content it sends out.   If for some reason you do not want to add this line of code to the php.ini, you can also change the default server behavior (uncompressed) by using the. htaccess file in the directory where the PHP source files are located, the syntax is as follows:         code is as follows :p Hp_value Output_handler Ob_gzhandler or is called from the PHP code, as follows:     code as follows: Ob_start ("Ob_gzhandler"); The method of using output-cache handles is really very effective and does not give the server any special load. However, it is important to note that Netscape Communicator has poor support for compressed graphics, so you should suppress JPEG and GIF graphics unless you can guarantee that all users will use IE. Generally, this compression works for all other files, but it is recommended that you test the various browsers individually, especially if you are using a special plugin or a data viewer. Note: 1, some Web server output_buffering default is 4069 characters or greater, that is, output must reach 4069 characters server will flush refresh output buffer, in order to ensure flush effective, preferably in Ob_flush () The preceding function has the following statement:   code is as follows: Print Str_repeat ("", 4096);  //to ensure that the Output_buffering value 2, ob_* series function is the output buffer that operates PHP itself, so ob_flush only refreshes the buffer of PHP itself, and flush is refreshing the Apache buffer. Therefore, the correct use of the two order is: first Ob_flush, and then flush. Ob_flush is the release of data from the PHP buffer, flush is to send the buffer inside/out of the data to the browser. 3, do not mistakenly believe that the use of Ob_start (), the script's echo/print output will never be displayed in the browser. Because the PHP script runs, it automatically refreshes the buffer and outputs the contents.  

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.