Instance description PHP dynamic content buffer and compression

Source: Internet
Author: User
PHP4 uses a buffer mechanism. Before you decide to send, all content is stored in the buffer, rather than directly sent to the browser. although you can use the header and setcookie functions, however, these two functions are just a little bit of "skill" compared to the powerful output functions ". Let's take a look at the true capabilities of these functions: voidob_start (vo PHP4 uses a buffer mechanism. Before you decide to send the function, all the content is stored in the buffer, rather than directly sent to the browser, although you can use the header and setcookie functions, these two functions are just a bit of a "trick" compared to the powerful output functions ". Let's take a look at the true capabilities of these functions:
Void ob_start (void );
This function instructs the PHP processor to redirect all output to the internal buffer. after this function is called, no output will be made to the browser.

String ob_get_contents (void );
This function returns the output buffer to a string. you can send the accumulated output together to the browser. Of course, you must first turn off the buffer.

Int ob_get_length (void );
This function returns the length of the output buffer.

Void ob_end_clean (void );
This function clears and disables the buffer. You need to use this function before outputting data to the browser.

Void ob_implicit_flush ([int flag])
This function is used to control implicit buffering and purging. the default value is off. if it is enabled, the results of each print/echo or output command are sent to the browser.


II. Use output control to compress PHP output
Before you start, make sure that your PHP4 compilation supports Zlib.
First, initialize the output buffer:

Ob_start ();
Ob_implicit_flush (0 );
?>

Then all output content is generated.

Print ("This example shows compressed output! ");
?>

After the page is generated, use:

$ Contents = ob_get_contents ();
Ob_end_clean ();
?>

We also need to check whether the browser supports data compression by checking "gzip, deflate" in the variable $ HTTP_ACCEPT_ENCODING:

If (ereg ('gzip, deflate', $ HTTP_ACCEPT_ENCODING )){

// Generate the content after gzip

} Else {

Echo $ contents;

}
?>

The following describes how to generate gzip output:

// Tell the browser that gzip data is received.
Header ("Content-Encoding: gzip ");
// Display the gzip file header

// Once is enough

Echo "x1fx8bx08x00x00x00x00x00 ";
// Calculate the length and CRC check code

$ Size = strlen ($ contents );

$ Crc = crc32 ($ contents );
// Compress data

$ Contents = gzcompress ($ contents, 9 );
// Content cannot be output directly here, because CRC has not been written!

$ Contents = substr ($ contents, 0, strlen ($ contents)-4 );

Echo $ contents;
Gzip_PrintFourChars ($ Crc );

Gzip_PrintFourChars ($ Size );
Function gzip_PrintFourChars ($ Val ){

For ($ I = 0; $ I <4; $ I ++ ){

Echo chr ($ Val %256 );

$ Val = floor ($ Val/256 );

}

}
?>

3. buffer PHP output
The buffer can be easily implemented in PHP4. let's look at the example:

// Generate a unique file name for the requested URI.

$ Cached_file = md5 ($ REQUEST_URI );
If ((! File_exists ("/cache/$ cached_file") | (! Is_valid ("/cache/$ cached_file "))){
Ob_start ();

Ob_implicit_flush (0 );
// Output the 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

The PHP output buffer function is very useful in script output operations. compressing the buffer can reduce the output time by 80%, which is used to access other data resources (such as databases or XML, it is also a good buffer mechanism.

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.