How to use output_buffering in PHP? _php Tutorials

Source: Internet
Author: User
Tags set cookie setcookie

How to use output_buffering in PHP?


This article mainly introduces the output_buffering of PHP in detail, this article explains some advanced usage of output buffering, the need for friends can refer to the following

I personally think that the Output buffering is a relatively pure 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

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:

The code 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. The HTTP header does not have too many rules, typically, it has the following format:

The code is as follows:

field:value[field: value]

You must use empty lines to separate them 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:

The code is as follows:

Header ("location:http://www.php.net/"); Redirect to http://www.php.net/

You can also use the Setcookie () function:

The code is as follows:

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

The code is as follows:

  

Setcookie ("foo", "Bar");

Print "Set cookie.";

?>

This is going to be the case:

The code is as follows:

http/1.1 OK

Date:sat, Sep 21:43:02 GMT

server:apache/1.3.11 (Unix) Mod_macro/1.1.1php/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

The output buffering technology was clearly needed 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 occur? If you try to add or modify header information after the HTTP header has been sent, and if there are blank lines between the body of the document and the title, these error messages are generated. 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-header output (for example, using a block or print () call), then 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.

  How the 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

You can use the following four functions, which can help you control the output buffering:

The code is as follows:

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 ()

Send output buffer (out buffering) and disable the output buffering mechanism.

Ob_end_clean ()

Clears output buffer but does not send, and disables output buffering.

http://www.bkjia.com/PHPjc/886045.html www.bkjia.com true http://www.bkjia.com/PHPjc/886045.html techarticle How to use output_buffering in PHP? This article mainly introduces the output_buffering of PHP in detail, this article explains some advanced usage of output buffering, the need for friends can refer to ...

  • Related Article

    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.