Phpob_start (ob_gzhandler) for web page compression

Source: Internet
Author: User
First, let's take a look at the ob_start usage and use the PHPob_start () function to open the browser cache. This ensures that the cache content is completed when you call flush (), ob_end_flush () (or the program execution is completed.

First, let's take a look at the ob_start usage. use the PHP ob_start () function to open the browser cache. This ensures that the cache content is displayed after you call flush (), ob_end_flush () (or program execution is completed) the code is as follows:

  1. Ob_start (); // open the buffer
  2. Phpinfo (); // use the phpinfo function
  3. $ Info = ob_get_contents (); // Obtain the buffer content and assign it to $ info
  4. Using file1_fopen('info.txt ', 'w'); // open the info.txt file.
  5. Fwrite ($ file, $ info); // write the information to info.txt
  6. Fclose ($ file); // Close the info.txt file
  7. ?>

PHP ob_start () functions have a great feature. you can also use the ob_start parameter to automatically run commands after the cache is written, such as ob_start ("ob_gzhandler "); the most common practice is to use ob_get_contents () to get the code of the content in the cache as an example of compressing the web page. We use the ob_gzip function, use ob_start to compress the output content and put it in the "buffer zone". the code is as follows:

  1. // Enable compression
  2. If (function_exists ('OB _ gzip '))
  3. {
  4. Ob_start ('OB _ gzip ');
  5. }
  6. // Prepare some content to be compressed
  7. For ($ I = 0; I I <100; $ I ++)
  8. {
  9. Echo ('Here is the test content.
    ');
  10. }
  11. // Output compressed results
  12. Ob_end_flush ();
  13. // This is the ob_gzip compression function.
  14. Function ob_gzip ($ content)
  15. {
  16. If (! Headers_sent () & extension_loaded ("zlib") & strstr ($ _ SERVER ["HTTP_ACCEPT_ENCODING"], "gzip ")){
  17. $ Content = gzencode ($ content, 9 );
  18. Header ("Content-Encoding: gzip ");
  19. Header ("Vary: Accept-Encoding ");
  20. Header ("Content-Length:". strlen ($ content ));
  21. }
  22. Return ($ content );
  23. }

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.