Efforts to accelerate PHP programs _php

Source: Internet
Author: User
Keywords effort program acceleration output buffering functions
Dynamic website content Acceleration display is very important, this article through the PHP several functions in-depth discussion, proposed the PHP Web page compression and the buffer solution

Introduction of several functions to control the output of PHP

PHP4 uses a buffering mechanism that, before you decide to send, everything is just in the buffer, not directly to the browser, although you can do it with the header and Setcookie functions, but these two functions are only a little "tricks" compared to the powerful output function. 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, and 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 that you can use to send the stacked output to the browser. Of course, you should 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 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 purging, default to OFF, and if open, the results of each print/echo or output command are sent to the browser.


Second, the output control to compress the output of PHP

Before you start, make sure that your PHP4 compile-time support for Zlib.
First, initialize the output buffers:


Ob_start ();
Ob_implicit_flush (0);
?>

It then produces all the 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 supports compressing data, we use the method of checking "gzip, deflate" in the variable $HTTP _accept_encoding:


if (Ereg (' gzip, deflate ', $HTTP _accept_encoding)) {
Content after the gzip is generated
} else {
Echo $contents;
}
?>

Below we analyze how to produce gzip output:


Tell the browser that the gzip data is received below.
Header ("Content-encoding:gzip");
Show file headers for gzip files
Just once is enough.
echo "x1fx8bx08x00x00x00x00x00";
Calculate length and CRC check code
$Size = strlen ($contents);
$CRC = CRC32 ($contents);
Compress data
$contents = Gzcompress ($contents, 9);
Cannot output content directly here, because the CRC has not been written yet!
$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);
}
}
?>

Third, buffer the output of PHP

It's easy to implement Buffering in PHP4, let's take a look at examples:


Produces 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);
Here the output buffer
$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 buffering function is useful for manipulating script output, which reduces output time by 80%, which is a good buffer for accessing other data resources such as databases or XML.
  • 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.