Go PHP Ob_start () and Ob_gzhandler ()

Source: Internet
Author: User
Tags file info html header phpinfo setcookie browser cache
The output control function gives you the freedom to control the outputs of the data in your script. It is very useful, especially for: when you want to
After the data has been output, the file header is then output. The output control function does not use header () or Setcookie (),
The file header information sent affects only those data blocks that resemble Echo () and PHP code.


Let's start with a simple example that gives you a general impression of the output control:


Example 1.
-------------------------------------------------------------------------------------
---------------------------------


Ob_start (); Open buffer


echo "hello\n"; Output


Header ("location:index.php");//redirect browser to index.php
Ob_end_flush ();//output all contents to browser
?>
----------- --------------------------------------------------------------------------
---------------------------------
all who know about the header () function know that this function sends a file header to the browser, but if you have any output before using the
function (including empty output, such as spaces, carriage returns, and line feeds), an error is indicated. If I
get rid of the first line of Ob_start () and execute the program, we will find an error message: "Header had
all ready to send by"! But with Ob_start, there is no hint of 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 to output, and the
To not have any file header output error!


Introduction of related functions:
1, Flush: Flush the contents of the buffer, output.
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 the contents of the Ob_end_flush () or flush () output buffers.
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, and, like ob_get_contents, if the output buffer is not stimulated
Live. 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 but deletes it!
7. Ob_implicit_flush: Turn absolute refresh on or off
How to use: void Ob_implicit_flush ([int flag])
Description: People who have used Perl know the meaning of $│=x, the 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, each script output is straight
Send to the browser, no longer need to call flush ()


Second, in-depth understanding:

1. About the flush function:
This function appears in PHP3, is a very efficient function, and he has a very useful function is to refresh the browser
cache. Let's take a very obvious example of how flush works.
Example 2.
-------------------------------------------------------------------------------------
------------------ ---------------
for ($i = 1; $i <=; $i + +) print ("");
//This sentence is very critical, the structure of the cache so that its content only to a certain size to output from the browser
//In other words, if the content of the cache does not reach a certain size, it will not be output before the completion of the program execution. After the
//test, I found that the limit of this size is 256 characters. This means that the content received by the cache will be
//sent continuously.
for ($j = 1; $j <=, $j + +) {
echo $j. "
";
Flush ();//This will cause the cache additions to be pushed out to the browser
Sleep (1);//Let the program "Sleep" a second, will let you see the effect more clearly
}
?>
--------------- ----------------------------------------------------------------------
---------------------------------
Note: If you open an absolute refresh by adding Ob_implicit_flush () to the program's header, you can no longer use flush
() in your program, and the benefit is: increase efficiency!

2. About OB series functions:
I'd like to cite an example of my good friend y10k first:
Example 3.
-------------------------------------------------------------------------------------
---------------------------------

For example, you can use the server and client settings information, but this information will vary depending on the client, if you want to
What about saving the output of the phpinfo () function? There's no way to say it without buffer control, but there's a
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 (' 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! actually on
Face is the way to turn some "process" into a "function"!

-------------------------------------------------------------------------------------
---------------------------------
Perhaps now you have a certain understanding of the function of Ob_start (), the above example seems simple, but in fact already palm
The main point of using Ob_start () is grasped.
<1>: Use Ob_start to open the browser cache, which ensures that the contents of the cache are in your call to flush
(), Ob_end_flush () will not be output until it is finished (or the program is executed).
<2> Now you should know the advantages you have: You can use Header,setcookie after any output and
Session, which is a big feature of Ob_start, you can also use the Ob_start parameter after the cache is written, and then
Automatically run commands such as Ob_start ("Ob_gzhandler"), and our most common practice is to use ob_get_contents () to
To the content in the cache and then process it ...
<3>: When the processing is complete, we can use various methods to output, flush (), Ob_end_flush (), and wait until the program finishes executing
After completion of the automatic output. Of course, if you use Ob_get_contents (), then you have to control the output mode.

Here, let's see what we can do with OB series functions ...

First, static template technology

Introduction: The so-called static template technology is in some way, so that users on the client side is generated by the PHP HTML page
Surface. If the HTML page is not updated again, the program will no longer invoke the page when another user browses to it again.
PHP and related databases, for some of the more informative sites, such as Sina,163,sohu. A technique like this
The benefits of coming are very huge.

There are two ways I know to implement static output:
<1>. A template.inc.php class implementation that modifies Phplib by y10k.
<2> Use the OB series function implementation.
For the first method, because it is not the issue to be studied in this article, so don't repeat it.
Let's take a look at the concrete implementation of the second approach:
Example 4.
-------------------------------------------------------------------------------------
---------------------------------
Ob_start ();//Open buffer
?>
Full output of PHP page
$content = Ob_get_contents ();//Get all the contents of the PHP page output
$fp = fopen ("output00001.html", "w"); Create a file, and open it, ready to write
Fwrite ($fp, $content); Write the contents of the PHP page to output00001.html, then ...
Fclose ($FP);
?>

================================================================================

The

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, header information header (), content type, and cookies do not use buffering. The

uses PHP in the process of using 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 newline) will prompt an error with the following message: "Header had all ready to send by"!. The

adds several functions of the buffer control to the PHP4.0. The

Function name ob_start
function format void Ob_start (void)
Function: Opens the output buffer.
Description: 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.

Instance Analysis:
1, buffer-controlled function to prevent file hair sending information error.

!--? PHP prompt
ob_start (); Open buffer
echo "welcome/n";//Output
Header ("location:next.php");//redirect browser to next.php
?>

If you remove Ob_star T,php will be prompted to error in the 4th line of the file, 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 is not output to the browser, Instead, it 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!

PHP4.0.4 has a new output cache handle Ob_gzhandler, similar to the previous class, but with a different usage. The content to be added to PHP.ini when using Ob_gzhandler is as follows:

Output_handler = Ob_gzhandler;


This line of code allows PHP to activate the output cache and compress everything it sends out. If for some reason you do not want to add this line of code to php.ini, you can also change the default server behavior (not compressed) by using the. htaccess file in the directory where the PHP source files are located, with the following syntax:

Php_value Output_handler Ob_gzhandler


Or it is called from the PHP code, as follows:

Ob_start ("Ob_gzhandler");


The method of using output cache handles is really effective and does not give the server any special load. It is important to note, however, that Netscape Communicator has poor support for compressed graphics, so you should suppress JPEG and GIF graphics unless you can ensure that all users are using Internet Explorer. Generally, this compression works for all other files, but it is recommended that you test separately for various browsers, especially if you are using a special plug-in or data viewer.

Using the various techniques described earlier, you can significantly improve the performance of your site, but it should be noted that:
PHP may or may not be a performance bottleneck. Be sure to look closely at each and every factor that applies to performance, such as a database.
Using this technique alone can only improve the performance of the Web server within a certain limit. So before blaming PHP and its cache, consider whether you should upgrade the server and whether you can introduce load balancing technology (which requires a large investment).
Do not underestimate the role of content compression. Although you see the Web app responding very quickly under a LAN connection of up to a few megabytes (MB/s), users who connect with the modem will not, they will only complain that your KB HTML page is too large.

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