Discussion on output buffering one of outputbuffering _php tutorials

Source: Internet
Author: User
Tags php print set cookie terminates
######################################### # #标题 Discussion on output buffering # #整理 Diego Lynn @ Forest National # #原作 by Zeev Suraski ##################### #################### about output buffering discussion (output buffering) directory HTTP Header Why use output buffering technology output buffering how to work basic usage advanced usage makes It's easier than that. I personally think that Output buffering is a relatively pure PHP 4.0 feature. Although conceptually quite simple, the output buffering feature is powerful enough to make it easier for developers to develop advanced and effective programs. This article describes the HTTP header, and how output buffering can help you with HTTP headers, and describes some of the advanced uses of output buffering. HTTP Header[http header] For each request that is established using the HTTP protocol, the response from the Web server typically consists of two parts – the title and the principal. For example, if there is a small text file under the Web server's document root, called Example.txt, and the file contains the text Hello, world!, then the HTTP request response to this file is as follows: http/1.1 OK Date:sat, Sep 21:40:08 GMT server:apache/1.3.11 (Unix) mod_macro/1.1.1 Php/4.0.2-dev Last-modified:sat, Sep 21:39:49 GMT ETag: "12600b-e-39b173a5" accept-ranges:bytes content-length:14 connection:close content-type:text/plain Hello, World ! The first part of the request (which is the larger part) is the HTTP header. Although the user does not see the HTTP header in the browser, it contains information for the browser, such as the document content type, the protocol version used, the date the document was last changed, and so on. HTTP header does not have too many rules, usually, it'sThe format is as follows: field:value[field: value] must be empty line they are separated from the document body. You can add or change the information for this HTTP header from a PHP script. For example, you can use the header () function: Header ("location:http://www.php.net/"); Redirection to http://www.php.net/can also be used with the Setcookie () function: Setcookie ("foo", "Bar"); You may know that HTTP cookies are implemented using HTTP headers. For example, the HTTP request response for the following PHP file Will be like this: http/1.1 OK Date:sat, the Sep 21:43:02 GMT server:apache/1.3.11 (Unix) mod_macro/1.1.1 Php/4.0.2-dev x Powered-by:php/4.0.2-dev set-cookie:foo=bar connection:close content-type:text/html Set Cookie. The browser reads the HTTP header returned from the server, knowing that a cookie called Foo is sent (here is a session cookie), and its value is bar. Why use output buffering technology is obviously required for output buffering technology as early as PHP/FI 2.0. If you have used this version of PHP, then you may remember often encounter Oops, Setcookie called after the header has been sent this error message, and make you take the head to grasp the ear, also can not understand what reason. If you have used the latest version of PHP-PHP 3.0 or PHP 4.0-then you will know this error message: Oops, Php_set_cookie called after header has been sent. Alternatively, you will encounter the cannot add header information-headers already sent message when you try to invoke the header () function of PHP. In general, output buffering technology users avoid these annoying error messages, and developers can use them for advanced purposes as well. When did these errors arise? These error messages are generated if you attempt to add or modify header information after an HTTP header has been sent, and when there is a lack of blank lines between the body of the document and the title. To understand how this is generated, let's look at how PHP handles the HTTP header output and the main output. When the script starts executing, it can send header and principal information at the same time. Header information (from the header () or Setcookie () function) is not sent immediately, instead it is saved to a list. This allows you to modify the header information, including the default title (such as the Content-type title). However, once the script sends any non-standard(for example, using an HTML code block or a print () call), PHP must send all headers before sending a blank line, terminating the HTTP header, and then continuing to send the principal data. From this point on, any attempt to add or modify header information is not allowed, and one of the above error messages is sent. While this does not cause much problem, sometimes it simply terminates the HTTP header before any input is emitted, which complicates the scripting logic. The Output buffering technology solves these problems. Output buffering works when output buffering is enabled, PHP does not send an HTTP header when the script sends out. Instead, it imports this output through a pipeline (pipe) into a dynamically increasing cache (only used in PHP 4.0, which has a centralized output mechanism). You can still modify, add a header row, or set a cookie because the caption is not actually sent. In the simplest case, when the script terminates, PHP will automatically send the HTTP header to the browser and then send the contents of the output buffer. It's easy. Basic usage can use the following four functions, which can help you control output Buffering:ob_start () enable the output buffering mechanism. Output buffering supports multiple levels--for example, the Ob_start () function can be called multiple times. Ob_end_flush () sends output buffer (out buffering) and disables the output buffering mechanism. Ob_end_clean () Clears output buffer but does not send, and disables output buffering. Ob_get_contents () returns the current output buffer as a string. Allows you to handle any output that the script emits. In addition, you can enable the Output_buffering directive in php.ini. If this directive is enabled, then each PHP script is equivalent to calling the Ob_start () function from the beginning. Example 1

Example 1

Here, even though you have sent the output (in the HTML code block and in the print statement), you can also use the Setcookie () call without error, and really thank the output buffering mechanism. Note that using the output buffering mechanism for this purpose can cause some performance penalty, so it is best not to enable this mechanism by default. However, for more complex scripts, the output buffering can simplify logic. Example 2 This example shows a very inefficient way to determine the length of a string. Instead of simply using the strlen () function, it first enables the output buffering mechanism, prints the string, and then determines the length of the output buffer. Finally clear output buffer (not sent), and then disable the output buffering mechanism.

http://www.bkjia.com/PHPjc/531718.html www.bkjia.com true http://www.bkjia.com/PHPjc/531718.html techarticle ######################################### # #标题 discussion on output buffering # #整理 Diego Lynn @ Forest National # #原作 by Zeev Suraski ################## ####################### about ...

  • 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.