_php tutorial on buffer control under PHP

Source: Internet
Author: User
Tags html header
PHP4.0 provides a collection of output buffering functions. Output buffering support allows you to write wrap function functions to compress buffers. Output buffering support in PHP4 allows HTML header information to be stored, regardless of whether the body of the HTML is output. However, in PHP, the header information (header (), content type, and cookies) does not use buffering.
In the process of using PHP, it is unavoidable to use the header and
Setcookie two functions, these two functions will send a file header information to the browser, but if you have any output before using these two functions (including null output, such as spaces, carriage return and line break) will prompt an error, the message is as follows: "Header had all ready send By "!. In PHP 4.0 added a buffer control of several functions, using these functions can help us solve a lot of problems.
 
Function name function Format Function description
Flush Flush () the contents of the output buffer and delete the buffer. This function is often used and is highly efficient.
Ob_start void Ob_start (void) to open the output buffer. 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.
Ob_get_contents string ob_get_contents (void) Returns the contents of the internal buffer. This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.
ob_get_length int ob_get_length (void) Returns the length of the internal buffer. This function returns the length of the current buffer, and, like ob_get_contents, if the output buffer is not activated. FALSE is returned.
Ob_end_flush void Ob_end_flush (void) sends the contents of the internal buffer to the browser and closes the output buffer. This function sends the contents of the output buffer (if any).
Ob_end_clean void Ob_end_clean (void) Deletes the contents of the internal buffer and closes the internal buffer. This function does not output the contents of the internal buffer!
Ob_implicit_flush void Ob_implicit_flush ([int flag]) turns on or off absolute refresh the person who used Perl knows the meaning of $|=x, the string can open/close the buffer, and Ob_implicit_ The flush function, like that, defaults to closing the buffer and opening the absolute output.
Second, the case analysis:
1, buffer control function to prevent the file hair send information error.
Ob_start (); Open buffer
echo "welcome/n"; Output
Header ("location:next.php"); redirect the browser to next.php
?>
If you remove ob_start,php, you will be prompted in the 4th line of the file error, the error message is "Header had all ready to send by", but with Ob_start, there is no prompt error, because when the buffer is opened, the character after the Echo will not be output to the browser , but remains in the server's buffer until you use flush or ob_end_flush to output, so there is no error in the file header output!
2, Save the output (this is a very classic use).
If you want to know the output of the client's screen output as a function, and the output information will vary depending on the client. We can use the function Get setup information for the server, but what if you want to save the output of the phpinfo () function? 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.
Ob_start (); Open buffer
Phpinfo (); Using the Phpinfo function
$info =ob_get_contents (); Get the contents of the buffer area and assign it to $info
$file =fopen (' Phpinfo.txt ', ' W '); Open File Phpinfo.txt
Fwrite ($file, $info); Write information to Phpinfo.txt
Fclose ($file); Close File Phpinfo.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 the same way, buffers can be used to save tasks that are difficult to accomplish in a general way, which is actually the method of translating some "processes" into "functions".

http://www.bkjia.com/PHPjc/446895.html www.bkjia.com true http://www.bkjia.com/PHPjc/446895.html techarticle PHP4.0 provides a collection of output buffering functions. Output buffering support allows you to write wrap function functions to compress buffers. Output buffering support in PHP4 allows HTML header information to be stored, regardless of ...

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