Home > Backend Development > PHP Tutorial > Introduction to methods of caching and compressing dynamic pages in PHP

Introduction to methods of caching and compressing dynamic pages in PHP

WBOY
Release: 2016-07-25 08:59:00
Original
1090 people have browsed it
  1. void ob_start(void);
  2. ?>
Copy code

tells the PHP processor to redirect all output to an internal cache (buffer). No output will be sent to the browser until ob_start is called.

  1. string ob_get_contents(void);
  2. ?>
Copy code

This function returns the "output buffer" as a string. You can call this function to send the accumulated output to the browser. (Only after turning off the buffering function!!)

  1. int ob_get_length(void);
  2. ?>
Copy code

Returns the length of the string in the cache.

  1. void ob_end_clean(void);
  2. ?>
Copy the code

Clear the output cache and turn the output cache off. This function must be used before the content in the cache is output to the browser. void 501([int flag]) Used to turn on/off the implicit flush action switch (default is off). If flush is turned on, every time print/echo or other output commands are called, the output content will be immediately sent to the browser.

Use output controls to compress PHP output You must use the Zlib extension package compiled in PHP4 to compress the output. If necessary, you can view the installation instructions for the Zlib package in the PHP documentation. First, initialize the output cache:

  1. ob_start();
  2. ob_implicit_flush(0);
  3. ?>
After copying the code

, use print, echo, or other methods you like to generate all output content ,For example:

  1. print("Hey this is a compressed output!");
  2. ?>
Copy the code

After the page is generated, we get back the output content:

  1. $contents = ob_get_contents();
  2. ob_end_clean();
  3. ?>
After copying the code

, you must check whether the browser supports compressed data. If supported, the browser will send an ACCEPT-ENCODEING HTTP header to the server. We only need to check if there is a "gzip,deflate" string in the $HTTP_ACCEPT_ENCODING variable.

  1. if(ereg('gzip, deflate',$HTTP_ACCEPT_ENCODING)) {
  2. // Generate Gzip compressed content here
  3. } else {
  4. echo $contents;
  5. }
  6. ?> ;
Copy code

This method is simple to use and clearly structured. 1 2 Next Page Last Page



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