PHP for the buffer control implementation code

  • 2020-09-16 07:23:50
  • OfStack

Everyone in the process of using PHP unavoidable should use to header and setcookie two functions, these two functions will send 1 file header information to the browser, but if before using these two functions have any output (including an empty output, such as space, and newline) will prompt wrong, prompt information is as follows: "Header had all ready send by"! So what are some ways to send header information when you have output? Several buffer control functions have been added to PHP 4.0, which can be used to solve many problems.

1. Introduction to relevant functions:

1. Flush: Output the contents of the buffer and delete the buffer.

Function format: flush()

Note: This function is frequently used and very efficient.

2. ob_start: Open output buffer

Function format: void ob_start(void)

Note: When the buffer is activated, all non-header information from the PHP program is not sent, but is stored in the internal buffer. To output the contents of the buffer, use ob_end_flush() or ob_end_clean() to output the contents of the buffer.

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

Usage: string ob_get_contents(void)

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

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

Usage: int ob_get_length(void)

Description: This function returns the length of the current buffer. Like ob_get_contents1, if the output buffer is not activated. Returns FALSE.

5. ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer.

Usage: void ob_end_flush(void)

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

6. ob_end_clean: Removes the contents of the internal buffer and closes the internal buffer

Usage: void ob_end_clean(void)

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

7. ob_implicit_flush: Turn absolute refresh on or off

Usage: void ob_implicit_flush ([int flag])

Note: Anyone who has used Perl knows this? The ob_implicit_flush function is the same as that one. The default is to close the buffer and turn on the absolute output.

2. Use Examples:

At the beginning of 1, The author said that using the buffer control function can prevent the file hair message error, the following is an example:


<? //PHP prompt 
ob_start(); // Open buffer 
echo "Hello/n"; // The output 
header('location:gotourl.php'); // Redirect the browser to gotourl.php
?>

If you remove ob_start PHP will prompt the file of line 4 error (error message as shown in the front), but add ob_start, won't prompt error, the reason is that when you open the buffer, the character after the echo not output to the browser, but kept on the server, until you use flush or ob_end_flush output, so will not have any output file header error!

Here's another classic use:

For example, you use < ?phpinfo();? > Get setup information for the server and client, but this information will vary from client to client. What if you want to save the output of the phpinfo() function? Before there was buffer control, there was no way to do it, but with buffer control, we could easily do it:


<?
ob_start(); // Open buffer 
phpinfo(); // use phpinfo function 
?$info=ob_get_contents(); // Gets the contents of the buffer and assigns to ?$info
?$file=fopen('info.txt','w'); // Open the file info.txt
fwrite(?$file,?$info); // Write information to info.txt
fclose(?$file); // Close the file info.txt
?>


With the above method, you can save phpinfo information of different users, which was probably not possible before! In fact, the above is how to convert 1 "process" into "function"!


Related articles: