Example to explain the buffering and compressing of PHP dynamic content
Source: Internet
Author: User
Dynamic | compression PHP4 uses a buffer mechanism , before you decide to send , All content just exists in the buffer , Instead of sending it directly to the browser , Although you can use header and Setcookie functions to achieve , but these two functions are only a little "tricks" compared to powerful output functions. Let's take a look at the true capabilities of these functions :
void Ob_start ( void ) ;
This function tells the PHP processor to redirect all output to the internal buffer , after calling this function , there will be no output to the browser.
string ob_get_contents ( void ) ;
This function returns the output buffer to a string , you can use to send stacked output together to the browser. Of course, turn off the buffer first.
int Ob_get_length ( void ) ;
This function returns the length of the output buffer.
void Ob_end_clean ( void ) ;
This function clears and closes the buffer. You need to use this function before outputting to the browser.
void Ob_implicit_flush ( [ int Flag ] )
This function is used to control the implicit buffer purging , default is off , If you open , for each Print / Echo or the results of the output command are sent to the browser.
second, the use of output control to compress the output of PHP
Before you begin , Make sure that your PHP4 supports Zlib at compile time.
First , Initializing output buffering :
Ob_start ( ) ;
Ob_implicit_flush ( 0 ) ;
?>
And then produce all the output content.
Print ( "This example is compressed output!" " ) ;
?>
After page generation , Adopt :
$contents = ob_get_contents ( ) ;
Ob_end_clean ( ) ;
?>
Also check to see if the browser supports compressed data , we use the variable $HTTP _accept_encoding to check "gzip, deflate" the approach :
It's easy to buffer in PHP4 . , Let's look at the example :
//The URI of the request produces a unique file name.
$cached _file=md5 ($REQUEST _uri);
if ((!file_exists ("/cache/$cached _file")) | | (!is_valid ("/cache/$cached _file")) {
Ob_start ();
Ob_implicit_flush (0);
The output buffer here
$contents = Ob_get_contents ();
Ob_end_clean ();
$fil =fopen ($cached _file, "w+");
Fwrite ($fil, $contents, $strlen ($contents));
Fclose ($fil);
}
ReadFile ($cached _file);
?>
Iv. Conclusion
PHP output buffering function is useful in manipulating script output , compress the buffer to reduce the output % the output time , this is for accessing other data resources ( such as databases or XML ) for , is also a good buffer mechanism.
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