Php enables gzip compression to accelerate the website

Source: Internet
Author: User
Tags gz file
Php enables gzip compression to accelerate the website

Note:

After the compressed files are cached on the server, the re-access will reduce the re-compression time and reduce the CPU usage. By setting the client file cache time, you can reduce the number of re-requests by more than 85%. Because the image is already in the compression format, it only sets the client cache time and does not compress the image.

Usage:

The server must support the gzip and Rewrite functions. Add the following code RewriteRule (.. Css $ |.. Js $ |.. Jpg $ |.. Gif $ |. *. png $) gzip. php? $1 Upload gzip. php to the root directory

4. create a cache folder in the root directory to ensure read/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 period
  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 the last modification 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 for storing the gz file to ensure that the file can be written
  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 the gz file path
  34. $ Ext = array_pop (explode ('.', $ filename); // you can specify the file type based on the suffix.
  35. $ Type = "Content-type: text/html"; // 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 the gz file exists
  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. // The file modification date is the same as that in the browser cache, and 304 is returned.
  69. Header ("HTTP/1.1 304 Not Modified ");
  70. // Send the client header
  71. Header ("Content-Encoding: gzip ");
  72. Sendheader ($ gmt_mtime, $ type );
  73. }
  74. Else
  75. {
  76. // Read the gz file output
  77. $ Content = file_get_contents ($ cache_filename );
  78. // Send the 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. {// No corresponding gz file
  88. $ Mtime = mktime ();
  89. $ Gmt_mtime = gmdate ('d, d m y h: I: S', $ mtime). 'gmt ';
  90. // Read the file
  91. $ Content = file_get_contents ($ filename );
  92. // Remove the blank part
  93. // $ Content = ltrim ($ content );
  94. // Compress the file content
  95. $ Content = gzencode ($ content, 9, $ gzip? FORCE_GZIP: FORCE_DEFLATE );
  96. // Send the client header
  97. Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
  98. Header ("Content-Encoding: gzip ");
  99. // Send data
  100. Echo $ content;
  101. // Write a 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. {// The output in Gzip mode is not used for processing. The principle is basically the same as above.
  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. // The file modification date is the same as that in the browser cache, and 304 is returned.
  119. Header ("HTTP/1.1 304 Not Modified ");
  120. // Send the 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 the 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. ?>


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.