PHP enable gzip compression acceleration website

Source: Internet
Author: User
Tags gz file browser cache

Description

After the server caches the compressed files, the re-accesses reduce the re-compression time and reduce the CPU usage. By setting the client file cache time, reduce the number of requests again, can be reduced by more than 85%. Picture because it is already compressed format, just set the client cache time, do not do compression processing.

How to use:

The server must support the Gzip,rewrite feature. Add the following code to the "Rewritebase/" line of the. htacess file Rewriterule (.. css$|.. js$|.. jpg$|.. gif$|. *.png$) gzip.php?$1 upload gzip.php to the root directory

4, the cache folder is built in the root directory, guaranteed to read and write.

  1. /**
  2. * @author Seraphim
  3. * @copyright 2012
  4. */
  5. //
  6. function Sendheader ($last _modified, $p _type, $content _length = 0)
  7. {
  8. Set the client cache validity time
  9. Header ("Expires:".) Gmdate ("D, D M Y h:i:s", Time () + 15360000). "GMT");
  10. Header ("cache-control:max-age=315360000");
  11. Header ("Pragma:");
  12. Set Last Modified Time
  13. Header ("last-modified:".) $last _modified);
  14. Set File type information
  15. Header ($p _type);
  16. Header ("Content-length:".) $content _length);
  17. }
  18. Define (' Abspath ', DirName (__file__). '/');
  19. $cache = true;
  20. $cachedir = ' cache/'; directory where GZ files are stored, ensuring writable
  21. if (Empty ($_server[' query_string '))
  22. Exit ();
  23. $gzip = strstr ($_server[' http_accept_encoding '), ' gzip ');
  24. if (empty ($gzip))
  25. $cache = false;
  26. $key = Array_shift (Explode ('? ', $_server[' query_string '));
  27. $key = Str_replace ('.. /', ', $key);
  28. $filename = Abspath. $key;
  29. $symbol = ' _ ';
  30. $rel _path = Str_replace (Abspath, ", DirName ($filename));
  31. $namespace = Str_replace ('/', $symbol, $rel _path);
  32. $cache _filename = Abspath. $cachedir. $namespace. $symbol. BaseName ($filename).
  33. '. Gz '; Generate GZ file path
  34. $ext = Array_pop (Explode ('. ', $filename)); Determine file type information by suffix
  35. $type = "content-type:text/html"; The default file type
  36. Switch ($ext)
  37. {
  38. Case ' CSS ':
  39. $type = "Content-type:text/css";
  40. Break
  41. Case ' JS ':
  42. $type = "Content-type:text/javascript";
  43. Break
  44. Case ' gif ':
  45. $cache = false;
  46. $type = "Content-type:image/gif";
  47. Break
  48. Case ' jpg ':
  49. $cache = false;
  50. $type = "Content-type:image/jpeg";
  51. Break
  52. Case ' PNG ':
  53. $cache = false;
  54. $type = "Content-type:image/png";
  55. Break
  56. Default
  57. Exit ();
  58. }
  59. if ($cache)
  60. {
  61. if (file_exists ($cache _filename))
  62. {//If there is a GZ file
  63. $mtime = Filemtime ($cache _filename);
  64. $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
  65. if (Isset ($_server[' http_if_modified_since ') && array_shift (Explode ('; ', $_server[' http_if_modified_ SINCE ']) = =
  66. $GMT _mtime))
  67. {
  68. Same as the file modification date in the browser cache, returns 304
  69. Header ("http/1.1 304 not Modified");
  70. Send Client Header
  71. Header ("Content-encoding:gzip");
  72. Sendheader ($gmt _mtime, $type);
  73. }
  74. Else
  75. {
  76. Read GZ file output
  77. $content = file_get_contents ($cache _filename);
  78. Send Client Header
  79. Sendheader ($gmt _mtime, $type, strlen ($content));
  80. Header ("Content-encoding:gzip");
  81. Send data
  82. Echo $content;
  83. }
  84. }
  85. Else
  86. if (file_exists ($filename))
  87. {//does not have a corresponding GZ file
  88. $mtime = Mktime ();
  89. $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
  90. Read file
  91. $content = file_get_contents ($filename);
  92. Remove the blank part
  93. $content = LTrim ($content);
  94. Compress file Contents
  95. $content = Gzencode ($content, 9, $gzip? Force_gzip:force_deflate);
  96. Send Client Header
  97. Sendheader ($gmt _mtime, $type, strlen ($content));
  98. Header ("Content-encoding:gzip");
  99. Send data
  100. Echo $content;
  101. Write file
  102. File_put_contents ($cache _filename, $content);
  103. }
  104. Else
  105. {
  106. Header ("http/1.0 404 Not Found");
  107. }
  108. }
  109. Else
  110. {//processing does not use the output in gzip mode. The principle of basic ibid.
  111. if (file_exists ($filename))
  112. {
  113. $mtime = Filemtime ($filename);
  114. $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
  115. if (Isset ($_server[' http_if_modified_since ') && array_shift (Explode ('; ', $_server[' http_if_modified_ SINCE ']) = =
  116. $GMT _mtime))
  117. {
  118. Same as the file modification date in the browser cache, returns 304
  119. Header ("http/1.1 304 not Modified");
  120. Send Client Header
  121. Sendheader ($gmt _mtime, $type, strlen ($content));
  122. Header ("Content-encoding:gzip");
  123. }
  124. Else
  125. {
  126. Read file output
  127. $content = file_get_contents ($filename);
  128. Send Client Header
  129. Sendheader ($gmt _mtime, $type, strlen ($content));
  130. Send data
  131. Echo $content;
  132. }
  133. }
  134. Else
  135. {
  136. Header ("http/1.0 404 Not Found");
  137. }
  138. }
  139. ?>
Copy Code
PHP, gzip
  • 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.