PHPob_start () and ob_gzhandler ()

Source: Internet
Author: User
Tags html header
The [convert] PHPob_start () and ob_gzhandler () Output Control functions allow you to freely Control the Output of data in the script. It is very useful, especially when you want
Output the file header after the data has been output. The output control function does not use header () or setcookie (),
The file header information sent has an impact on data blocks that are similar to echo () and PHP code.


Let's take a simple example to give you a general impression on Output Control:


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


Ob_start (); // open the buffer


Echo "Hello \ n"; // output


Header ("location: index. php"); // redirects the browser to index. php.
Ob_end_flush (); // output all content to the browser
?>
Bytes -------------------------------------------------------------------------------------
---------------------------------
Anyone who knows about the header () function knows that this function will send a file header to the browser.
If this function already has any output (including empty output, such as space, carriage return, and line feed), an error will be prompted. If I
Remove ob_start () from the first line, and then execute this program. we will find an error message: "Header had
All ready send "! However, when ob_start is added, no error will be prompted, because when the buffer zone is opened
Will not be output to the browser, but will be retained on the server until you use flush or ob_end_flush.
So there will be no file header output errors!


1. related functions:
1. Flush: refresh the buffer content and output it.
Function format: flush ()
Note: This function is frequently used and highly efficient.
2. ob_start: Open the output buffer.
Function format: void ob_start (void)
Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone.
To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.
3. ob_get_contents: returns the content of the internal buffer.
Usage: string ob_get_contents (void)
Note: This function returns the content in the current buffer. if the output buffer is not activated, FALSE is returned.
4. ob_get_length: return the length of the internal buffer.
Usage: int ob_get_length (void)
Note: This function returns the length of the current buffer. it is the same as ob_get_contents. if the output buffer is not excited
Active. Returns FALSE.
5. ob_end_flush: sends the content of the internal buffer to the browser and closes the output buffer.
Usage: void ob_end_flush (void)
Note: This function sends the content of the output buffer (if any ).
6. ob_end_clean: delete the content of the internal buffer and disable the internal buffer.
Usage: void ob_end_clean (void)
Note: This function will not output the content of the internal buffer but delete it!
7. ob_implicit_flush: enable or disable absolute refresh
Usage: void ob_implicit_flush ([int flag])
Note: Anyone who has used Perl knows the meaning of $ │ = x. This string can enable/disable the buffer, while
The ob_implicit_flush function is the same as that function. the buffer is disabled by default. after the absolute output is enabled, the output of each script is straight.
Sent to the browser without calling flush ()


2. In-depth understanding:

1. about the Flush function:
This function appears in PHP3. it is a very efficient function. it has a very useful function that is to refresh the browser.
To illustrate the flush.
Example 2.
Bytes -------------------------------------------------------------------------------------
---------------------------------
For ($ I = 1; $ I <= 300; $ I ++) print ("");
// This sentence is critical. the cache structure makes it output from the browser only when the content reaches a certain size.
// In other words, if the cache content does not reach a certain size, it will not be output before the program is executed. Jing
// After testing, I found that the base size is 256 characters long. This means that all content received by the cache will be
// Continuously sent out.
For ($ j = 1; $ j <= 20; $ j ++ ){
Echo $ j ."
";
Flush (); // This part will squeeze out the new content of the cache and display it on the browser.
Sleep (1); // setting the program to "sleep" for one second will make the effect clearer.
}
?>
Bytes -------------------------------------------------------------------------------------
---------------------------------
Note: If you add ob_implicit_flush () to the program header to enable absolute refresh, you can stop using flush in the program.
(), The advantage of doing so is: Improve efficiency!

2. ob functions:
I would like to first reference an example of my good friend y10k:
Example 3.
Bytes -------------------------------------------------------------------------------------
---------------------------------

For example, you can obtain the configuration information of the server and the client, but the information varies depending on the client.
What should I do if I save the output of the phpinfo () function? There is no way to do this before there is buffer control.
We can easily solve the following problems:
-------------------------------------------------------------
Ob_start (); // open the buffer
Phpinfo (); // use the phpinfo function
$ Info = ob_get_contents (); // Obtain the buffer content and assign it to $ info
Using file1_fopen('info.txt ', 'w'); // open the info.txt file.
Fwrite ($ file, $ info); // write the information to info.txt
Fclose ($ file); // Close the info.txt file
?>
-------------------------------------------------------------
With the above method, you can save the phpinfo information of different users. I am afraid there is no way to do this before! Actually
Is to convert some "procedures" into "functions!

Bytes -------------------------------------------------------------------------------------
---------------------------------
Now you may have some knowledge about the functions of ob_start (). the above example seems simple, but it is actually quite easy.
Hold the key points of using ob_start.
<1>. use ob_start to open the browser cache. This ensures that the cache content is displayed when you call flush.
(), Ob_end_flush () (or program execution completed) will not be output before.
<2>. now you should know your advantages: you can use header, setcookie, and
Session, which is a major feature of ob_start. you can also use the ob_start parameter. after the cache is written
Automatically run commands, such as ob_start ("ob_gzhandler"). The most common practice is to use ob_get_contents ()
And then process the content in the cache ......
<3>. after processing, we can use various methods to output, flush (), ob_end_flush (), and wait until the program is executed.
Automatic output after completion. Of course, if you use ob_get_contents (), you can control the output mode by yourself.

Let's see what we can do with ob functions ......

I. static template technology

Introduction: the static template technology allows users to obtain html pages generated by PHP on the client.
. If the html page is no longer updated, the program will not call it when another user browses the page again.
PHP and related databases, for some websites with a large amount of information, such as sina, 163, sohu. Similar technologies
The benefits are enormous.

I know there are two ways to implement static output:
<1>. use the template. inc. php class modified by y10k to implement phplib.
<2>. use ob functions.
For the first method, because it is not a problem to be studied in this article, I will not go into details.
Let's take a look at the specific implementation of the second method:
Example 4.
Bytes -------------------------------------------------------------------------------------
---------------------------------
Ob_start (); // open the buffer
?>
All php page output
$ Content = ob_get_contents (); // retrieves all the content output on the php page.
$ Fp = fopen(export output00001.html "," w "); // create a file, open it, and prepare to write
Fwrite ($ fp, $ content); // write the content of the PHP page into output00001.html, and then ......
Fclose ($ fp );
?>

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

PHP4.0 provides a set of output buffer functions. The output buffer allows you to write the function to compress the buffer. The output buffer in PHP4 supports storing HTML header information, regardless of whether the HTML body is output or not. However, in PHP, header information header (), content type, and cookies are not buffered.

When using PHP, the header and setcookie functions are used. These two functions send a file header to the browser, however, if any output (including empty output, such as space, carriage return, or line feed) has been obtained before the two functions are used, an error is Prompted. The message is as follows: "Header had all ready send "!.

Added several buffer control functions in PHP4.0.

Function name ob_start
Function format void ob_start (void)
Function: enable the output buffer.
Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or ob_end_clean () to output the buffer content.

Instance analysis:
1. Use a buffer-controlled function to prevent information sending errors in the file header.

Ob_start (); // open the buffer
Echo "Welcome/n"; // output
Header ("location: next. php"); // redirects the browser to next. php
?>

If ob_start is removed, PHP will prompt an error in row 4th of the file. the error message is "Header had all ready send by". However, when ob_start is added, no error will be prompted, the reason is that when the buffer is opened, the characters after echo will not be output to the browser, but will be kept in the server's buffer until you use flush or ob_end_flush, so there will be no output errors in the file header!

PHP4.0.4 has a new output cache handle, ob_gzhandler, which is similar to the previous class but has different usage. When using ob_gzhandler, add the following content to php. ini:

Output_handler = ob_gzhandler;


This line of code enables PHP to activate the output cache and compress all the content it sends. If you do not want to add this line of code in php. ini for some reason, you can also change the default server behavior (not compressed) through the. htaccess file in the directory where the PHP source file is located. The syntax is as follows:

Php_value output_handler ob_gzhandler


Or you can call it from the PHP code as follows:

Ob_start ("ob_gzhandler ");


The method of using the output cache handle is indeed very effective and does not impose any special load on the server. However, it must be noted that Netscape Communicator does not provide good support for compressed images. Therefore, unless you can ensure that all users use IE browsers, you should disable JPEG and GIF image compression. Generally, this compression is effective for all other files, but we recommend that you perform tests on different browsers, this is especially important when you use a special plug-in or data viewer.

Using the various technologies described above, you can significantly improve the performance of your website, but note the following:
PHP may or may not be a performance bottleneck. Be sure to carefully observe every factor related to application performance, such as databases.
Simply using this technology can only improve the performance of Web servers to a certain extent. Therefore, before you blame PHP and its cache, you may want to see whether you should upgrade the server and whether the server load balancer technology can be introduced (the latter requires a large investment ).
Do not underestimate the effect of content compression. Although you can see that the Web application responds very quickly under the 100 Mbit/s LAN connection, the user who uses the Modem connection will not, they will only complain that your 100 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.