The control of buffer under PHP

Source: Internet
Author: User
Tags flush html header phpinfo


PHP4.0 provides a collection of output buffering functions. Output buffering support allows you to write the package function function to compress the buffer. The output buffering support in PHP4 allows the HTML header information to be stored, regardless of whether the HTML body is output or not. In PHP, however, 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 that send a header message to the browser, but if you have any output (including null output, such as a space, carriage return, and linefeed) before using the two functions, you will be prompted with an error, and the message is as follows: "Header had all ready send By "!. Several functions of buffer control are added to PHP 4.0, and these functions can help us solve many problems.



Function name function Format Function description



Flush the contents of the Flush () output buffer and deletes the buffer. This function is frequently used and is highly efficient.



Ob_start void Ob_start (void) opens the output buffer. When the buffer is activated, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. 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, FALSE if the output buffer is not active.



ob_get_length int ob_get_length (void) Returns the length of the internal buffer. This function returns the length of the current buffer, as ob_get_contents if the output buffer is not active. Returns FALSE.



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]) turn on or off absolute refresh anyone who has ever used Perl knows the meaning of $|=x, a string that can open/close a buffer, and ob_implicit_ The flush function is the same as that, by default the buffer is turned off and the absolute output is turned on.



Second, the case analysis:



1, with the buffer control function to prevent file hair send information error.



<? // PHP prompt
ob_start (); // Open the 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 file line 4th error, the error message is "Header had all ready send by", but plus ob_start, you will not be prompted for an error, because when the buffer is open, the characters behind Echo will not 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 client's screen output information like the output of the function and so on, and this output information will vary depending on the client. We can use the function. Phpinfo ();?> Get server Setup information, but what if you want to save the output of the phpinfo () function? Before there is no buffer control, we can say that there is no way, but with the control of the buffer, we can easily solve.



<?
ob_start(); // Open buffer ob_start (); // Open the buffer
phpinfo (); // Use phpinfo function
$ info = ob_get_contents (); // Get the contents of the buffer and assign it to $ info
$ file = fopen ('phpinfo.txt', 'w'); // Open the file phpinfo.txt
fwrite ($ file, $ info); // write information to phpinfo.txt
fclose ($ file); // close the file phpinfo.txt
?>
?>



Using the above method, can be different users of the Phpinfo information to save, which in the past I am afraid to do! Similarly, the method of using the buffer can save the task that the general method is difficult to complete, which is actually the method of transforming some "process" into "function".


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.