Code for the control of buffers in PHP

Source: Internet
Author: User
Tags file info
Code for Buffer control implementation in PHP

In the process of using PHP to use the header and Setcookie two functions, these two functions will send a file header information to the browser, but if the two functions before the use of any output (including null output, such as space, carriage return and line break) will prompt an error, The message is as follows: "Header had all ready to send by"! What is the way to send the file header information in the case of output? In PHP 4.0 added a buffer control of several functions, using these functions can help us solve a lot of problems.

Introduction of related functions:

1. Flush: Outputs the contents of the buffer and deletes the buffer.

function format: Flush ()

Description: This function is often used and is highly efficient.

2. Ob_start: Open Output buffer

function format: void Ob_start (void)

Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use Ob_end_flush () or use Ob_end_clean () to output the contents of the buffer.

3. Ob_get_contents: Returns the contents of the internal buffer.

How to use: String ob_get_contents (void)

Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.

4. Ob_get_length: Returns the length of the internal buffer.

How to use: int ob_get_length (void)

Note: This function returns the length of the current buffer, as with ob_get_contents, if the output buffer is not activated. FALSE is returned.

5. Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer.

How to use: void Ob_end_flush (void)

Description: This function sends the contents of the output buffer (if any).

6. Ob_end_clean: Delete the contents of the internal buffer and close the internal buffer

How to use: void Ob_end_clean (void)

Description: This function does not output the contents of the internal buffer!

7. Ob_implicit_flush: Turn absolute refresh on or off

How to use: void Ob_implicit_flush ([int flag])

Description: Anyone who has ever used Perl knows? $|=x meaning, this string can open/close the buffer, and the Ob_implicit_flush function is the same as that, the default is to close the buffer, open the absolute output.

Ii. Examples of Use:

In the beginning, the author said that using buffer control function can prevent the file hair send information error, here is an example:

?

.The code is as follows:


Ob_start (); Open buffer
echo "hello/n"; Output
Header (' location:gotourl.php '); redirect the browser to gotourl.php
?>

?

If you remove ob_start,php, you will be prompted to make an error in the 4th line of the file (the error message is as shown earlier), but with Ob_start, there is no hint of an error, because when the buffer is opened, the character behind the echo is not output to the browser, but remains on the server. Until you use flush or ob_end_flush will not output, so there will be no file header output error!

Here is a very classic use:

For example, you use the server and client settings information, but this information is different from the client, if you want to save the Phpinfo () function output what to do? Before there is no buffer control, it can be said that there is no way, but with the control of the buffer, we can easily solve:

?

.The code is as follows:


Ob_start (); Open buffer
Phpinfo (); Using the Phpinfo function
? $info =ob_get_contents (); Get the contents of the buffer and assign the value to $info
? $file =fopen (' Info.txt ', ' W '); Open File Info.txt
Fwrite (? $file,? $info); Write information to Info.txt
Fclose (? $file); Close File Info.txt
?>

?


Using the above method, you can save the Phpinfo information of different users, which in the past I am afraid there is no way to do! In fact, the above is a few "process" into the "function" Method!


Another: The image generated by the verification code, in the image output to the browser, to clear the lower buffer
???????? Clear buffers
??????? Ob_clean ();
??????? Set file header;?
??????? Header ("Content-type:image/png");

??????? Export the image to a browser or file in PNG format;
??????? Imagepng ($distortion _im);

??????? Destroys an image, releasing the memory associated with image;
??????? Imagedestroy ($distortion _im);
??????? Imagedestroy ($im);

?

Source: http://www.poluoluo.com/jzxy/201309/243650.html

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