Home >Backend Development >PHP Tutorial >php enables gzip compression to accelerate website

php enables gzip compression to accelerate website

WBOY
WBOYOriginal
2016-07-25 08:45:071026browse
php, gzip


Description:

The compressed files are cached on the server, which reduces re-compression time and CPU usage when accessed again. By setting the client file cache time and reducing the number of re-requests, it can be reduced by more than 85%. Because the image is already in a compressed format, only the client cache time is set and no compression is performed.

How to use:

The server must support gzip and Rewrite functions. Add the following code RewriteRule (..css$|..js$|..jpg$|..gif$|.*.png$) gzip.php in the line below "RewriteBase /" in the .htacess file ?$1 Upload gzip.php to the root directory

4. Create a cache folder in the root directory to ensure it is readable and writable.

  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 the last modification time
  13. header("Last-Modified: " . $last_modified);
  14. // Set the 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 /'; //The directory where gz files are stored must be 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)); //Judge file type information based on 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 a 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. // with files in browser cache The modification date is consistent and 304 is returned. header("HTTP/1.1 304 Not Modified");
  69. // Send client header
  70. header("Content-Encoding:gzip");
  71. sendheader($gmt_mtime, $type);
  72. }
  73. else
  74. {
  75. // Read gz file output
  76. $content = file_get_contents($cache_filename);
  77. // Send client header
  78. sendheader($gmt_mtime, $type, strlen($content));
  79. header("Content -Encoding: gzip");
  80. // Send data
  81. echo $content;
  82. }
  83. }
  84. else
  85. if (file_exists($filename))
  86. { // There is no corresponding gz file
  87. $mtime = mktime();
  88. $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
  89. // Read the file
  90. $content = file_get_contents($filename);
  91. // Remove the blank part
  92. // $content = ltrim($content);
  93. // Compress file content
  94. $content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
  95. // Send client header
  96. sendheader($gmt_mtime, $type, strlen($content));
  97. header("Content-Encoding: gzip");
  98. // Send data
  99. echo $content;
  100. // Write file
  101. file_put_contents($cache_filename, $content);
  102. }
  103. else
  104. {
  105. header("HTTP/1.0 404 Not Found");
  106. }
  107. }
  108. else
  109. { // Process output in Gzip mode. The principle is basically the same as above
  110. if (file_exists($filename))
  111. {
  112. $mtime = filemtime($filename);
  113. $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
  114. if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) ==
  115. $gmt_mtime))
  116. {
  117. // with files in browser cache The modification date is consistent and 304 is returned. header("HTTP/1.1 304 Not Modified");
  118. // Send client header
  119. sendheader($gmt_mtime, $type, strlen($content));
  120. header("Content-Encoding: gzip");
  121. }
  122. else
  123. {
  124. // Read file output
  125. $content = file_get_contents($filename);
  126. // Send client header
  127. sendheader($gmt_mtime, $type, strlen($content));
  128. // Send data
  129. echo $content;
  130. }
  131. }
  132. else
  133. {
  134. header("HTTP/1.0 404 Not Found");
  135. }
  136. }
  137. ?>
Copy code

Statement:
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