Home > Backend Development > PHP Tutorial > Working hard to speed up PHP programs_PHP Tutorial

Working hard to speed up PHP programs_PHP Tutorial

WBOY
Release: 2016-07-20 11:03:38
Original
962 people have browsed it

It is very important to accelerate the display of content on dynamic websites. This article proposes a solution for PHP web page compression and buffering through an in-depth discussion of several PHP functions
1. Introducing several functions that control PHP output
PHP4 uses buffering Mechanism, before you decide to send, all content only exists in the buffer, rather than being sent directly to the browser. Although you can use the header and setcookie functions to achieve this, these two functions are not as powerful as the output function. Just a little "little trick". Let's take a look at the real 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. device.
string ob_get_contents(void);
This function returns the output buffer to a string, which you can use to send the stacked output together to the browser. Of course you have to turn off buffering 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 implicit buffer flushing. The default is off. If it is turned on, the results of each print/echo or output command will be sent to the browser. .
2. Use output control to compress PHP output
Before you start, make sure your PHP4 compiler supports Zlib.
First, initialize the output buffer:
ob_start();
ob_implicit_flush(0);
Then generate all output content.
print("This example is compressed output!");
After the page is generated, use:
$contents = ob_get_contents();
ob_end_clean();
Also check whether the browser To support compressed data, we use the method of checking "gzip, deflate" in the variable $HTTP_ACCEPT_ENCODING:
if(ereg('gzip, deflate',$HTTP_ACCEPT_ENCODING)) {
// Generate the content after gzip
} else {
echo $contents;
}
Let’s analyze how to generate gzip output:
// Tell the browser that the following is receiving gzip data.
header("Content-Encoding: gzip");
// Display the file header of the gzip file
// Just once is enough
echo "x1fx8bx08x00x00x00x00x00";
// Calculate the length and CRC check code
$Size = strlen($contents);
$Crc = crc32($contents);
// Compressed data
$contents = gzcompress($contents, 9);
// The content cannot be output directly here because the CRC has not been written yet!
$contents = substr($contents, 0, strlen($contents) - 4);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445258.htmlTechArticleIt is very important to accelerate the content display of dynamic websites. This article proposes a PHP web page through an in-depth discussion of several PHP functions. Compression and buffering solutions 1. Introduce several ways to control PHP output...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template