Efforts to accelerate PHP programs

Source: Internet
Author: User
Tags strlen
It is very important to accelerate the display of dynamic website content. Through in-depth discussion of several PHP functions, this article proposes a solution for PHP web page compression and buffering.
I. Introduce several functions that control PHP output.
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:
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 );

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.