国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php使用gzip壓縮傳輸js和css文件的方法

2024-05-04 23:38:13
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php使用gzip壓縮傳輸js和css文件的方法,實例分析了使用gzip實現(xiàn)壓縮js和css文件的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php使用gzip壓縮傳輸js和css文件的方法。分享給大家供大家參考。具體如下:

 

 
  1. <?php 
  2. /** 
  3. * 完整調(diào)用示例: 
  4. * 1、combine.php?t=j&b=public&fs=jslib.jquery,function 
  5.  
  6. * 該例子調(diào)用的是網(wǎng)站根目錄下的public/jslib/jquery.js和public/function.js 
  7.  
  8. * 2、combine.php?t=j&fs=jslib.jquery,function 
  9.  
  10. * 該例子調(diào)用的是網(wǎng)站根目錄下的jslib/jquery.js和function.js 
  11.  
  12. * 3、combine.php?t=c&b=public.css&fs=common,index 
  13.  
  14. * 該例子調(diào)用的是網(wǎng)站根目錄下的public/css/common.css和public/css/index.css 
  15.  
  16. * 4、combine.php?t=c&fs=css.common 
  17. * 該例子調(diào)用的是網(wǎng)站根目錄下的css/common.css 
  18.  
  19. * 注:多個文件名之間用,分隔;只有一個文件名最后不要有, 
  20. * 用,分隔的多個文件會被壓縮進一個文件,一次性傳給瀏覽器 
  21. **/ 
  22. $is_bad_request=false
  23. $cache = true
  24. $doc_root_uri=$_SERVER['DOCUMENT_ROOT'].'/'
  25. $cachedir = $doc_root_uri . 'public/cache'
  26. //文件類型,j為js,c為css 
  27. $type=isset($_GET['t'])?($_GET['t']=='j'||$_GET['t']=='c'?$_GET['t']:''):''
  28. //存放js和css文件的基目錄, 例如:?b=public.js 代表的是/public/js文件夾,出發(fā)點是網(wǎng)站根目錄 
  29. //基目錄參數(shù)不是必須的,如果有基目錄那么這個基目錄就會附加在文件名之前 
  30. $base =isset($_GET['b'])?($doc_root_uri.str_replace('.','/',$_GET['b'])):$doc_root_uri; 
  31. //文件名列表,文件名不帶后綴名.比如基目錄是 
  32. //文件名的格式是 :基目錄(如果有)+文件包名+文件名 
  33. //例如:類型是j, 
  34. // 文件名public.js.jquery 
  35. // 如果有基路徑且為public, 
  36. // 那么轉(zhuǎn)換后的文件名就是/public/public/js/jquery.js 
  37. // 如果沒有基路徑 
  38. // 那么轉(zhuǎn)換后的文件名就是/public/js/jquery.js 
  39. //多個文件名之間用,分隔 
  40. $fs=isset($_GET['fs'])?str_replace('.','/',$_GET['fs']):''
  41. $fs=str_replace(',','.'.($type=='j'?'js,':'css,'),$fs); 
  42. $fs=$fs.($type=='j'?'.js':'.css'); 
  43. if($type==''||$fs==''){$is_bad_request=true;} 
  44. //die($base); 
  45. if($is_bad_request){header ("HTTP/1.0 503 Not Implemented");} 
  46. $file_type=$type=='j'?'javascript':'css'
  47. $elements = explode(',',preg_replace('/([^?]*).*/''/1', $fs)); 
  48. // Determine last modification date of the files 
  49. $lastmodified = 0; 
  50. while (list(,$element) = each($elements)) { 
  51. $path =$base . '/' . $element; 
  52. if (($type == 'j' && substr($path, -3) != '.js') ||  
  53. ($type == 'c' && substr($path, -4) != '.css')) { 
  54. header ("HTTP/1.0 403 Forbidden"); 
  55. exit;  
  56. if (substr($path, 0, strlen($base)) != $base || !file_exists($path)) { 
  57. header ("HTTP/1.0 404 Not Found"); 
  58. exit; 
  59. $lastmodified = max($lastmodified, filemtime($path)); 
  60. // Send Etag hash 
  61. $hash = $lastmodified . '-' . md5($fs); 
  62. header ("Etag: /"" . $hash . "/""); 
  63. if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&  
  64. stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"')  
  65. // Return visit and no modifications, so do not send anything 
  66. header ("HTTP/1.0 304 Not Modified"); 
  67. header ("Content-Type: text/" . $file_type); 
  68. header ('Content-Length: 0'); 
  69. }  
  70. else 
  71. // First time visit or files were modified 
  72. if ($cache)  
  73. // Determine supported compression method 
  74. $gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'); 
  75. $deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate'); 
  76. // Determine used compression method 
  77. $encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none'); 
  78. // Check for buggy versions of Internet Explorer 
  79. if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') &&  
  80. preg_match('/^Mozilla//4/.0 /(compatible; MSIE ([0-9]/.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) { 
  81. $version = floatval($matches[1]); 
  82. if ($version < 6) 
  83. $encoding = 'none'
  84. if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1'))  
  85. $encoding = 'none'
  86. // Try the cache first to see if the combined files were already generated 
  87. $cachefile = 'cache-' . $hash . '.' . $file_type . ($encoding != 'none' ? '.' . $encoding : ''); 
  88. if (file_exists($cachedir . '/' . $cachefile)) { 
  89. if ($fp = fopen($cachedir . '/' . $cachefile, 'rb')) { 
  90. if ($encoding != 'none') { 
  91. header ("Content-Encoding: " . $encoding); 
  92. header ("Content-Type: text/" . $file_type); 
  93. header ("Content-Length: " . filesize($cachedir . '/' . $cachefile)); 
  94. fpassthru($fp); 
  95. fclose($fp); 
  96. exit; 
  97. // Get contents of the files 
  98. $contents = ''
  99. reset($elements); 
  100. while (list(,$element) = each($elements)) { 
  101. $path = $base . '/' . $element; 
  102. $contents .= "/n/n" . file_get_contents($path); 
  103. // Send Content-Type 
  104. header ("Content-Type: text/" . $file_type); 
  105. if (isset($encoding) && $encoding != 'none')  
  106. // Send compressed contents 
  107. $contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE); 
  108. header ("Content-Encoding: " . $encoding); 
  109. header ('Content-Length: ' . strlen($contents)); 
  110. echo $contents; 
  111. }  
  112. else 
  113. // Send regular contents 
  114. header ('Content-Length: ' . strlen($contents)); 
  115. echo $contents; 
  116. // Store cache 
  117. if ($cache) { 
  118. if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) { 
  119. fwrite($fp, $contents); 
  120. fclose($fp); 

希望本文所述對大家的php程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长乐市| 武穴市| 资兴市| 都江堰市| 灵山县| 蓬莱市| 牡丹江市| 盐亭县| 塔河县| 吉木乃县| 云阳县| 柯坪县| 凤山市| 林芝县| 临桂县| 滕州市| 利津县| 景东| 阳朔县| 兴山县| 临潭县| 莱西市| 运城市| 象州县| 长春市| 伊春市| 陇川县| 房山区| 江阴市| 东平县| 武陟县| 乌鲁木齐县| 淮安市| 鹤岗市| 禹城市| 犍为县| 东城区| 宁城县| 团风县| 莱州市| 莱州市|