亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2015年07月29日 11:04:28   作者:DDIAN  
這篇文章主要介紹了php使用gzip壓縮傳輸js和css文件的方法,實例分析了使用gzip實現(xiàn)壓縮js和css文件的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

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

<?php
  /**
   * 完整調(diào)用示例:
   * 1、combine.php?t=j&b=public&fs=jslib.jquery,function
   * 
   * 該例子調(diào)用的是網(wǎng)站根目錄下的public/jslib/jquery.js和public/function.js
   * 
   * 2、combine.php?t=j&fs=jslib.jquery,function
   * 
   * 該例子調(diào)用的是網(wǎng)站根目錄下的jslib/jquery.js和function.js
   * 
   * 3、combine.php?t=c&b=public.css&fs=common,index
   * 
   * 該例子調(diào)用的是網(wǎng)站根目錄下的public/css/common.css和public/css/index.css
   * 
   * 4、combine.php?t=c&fs=css.common
   * 該例子調(diào)用的是網(wǎng)站根目錄下的css/common.css
   * 
   * 注:多個文件名之間用,分隔;只有一個文件名最后不要有,
   *   用,分隔的多個文件會被壓縮進(jìn)一個文件,一次性傳給瀏覽器
   **/
  $is_bad_request=false;
  $cache = true;
  $doc_root_uri=$_SERVER['DOCUMENT_ROOT'].'/';
  $cachedir = $doc_root_uri . 'public/cache';
  //文件類型,j為js,c為css
  $type=isset($_GET['t'])?($_GET['t']=='j'||$_GET['t']=='c'?$_GET['t']:''):'';
  //存放js和css文件的基目錄, 例如:?b=public.js 代表的是/public/js文件夾,出發(fā)點是網(wǎng)站根目錄
  //基目錄參數(shù)不是必須的,如果有基目錄那么這個基目錄就會附加在文件名之前
  $base =isset($_GET['b'])?($doc_root_uri.str_replace('.','/',$_GET['b'])):$doc_root_uri;
  //文件名列表,文件名不帶后綴名.比如基目錄是
  //文件名的格式是 :基目錄(如果有)+文件包名+文件名
  //例如:類型是j,
  //   文件名public.js.jquery
  //   如果有基路徑且為public,
  //   那么轉(zhuǎn)換后的文件名就是/public/public/js/jquery.js
  //   如果沒有基路徑
  //   那么轉(zhuǎn)換后的文件名就是/public/js/jquery.js
  //多個文件名之間用,分隔
  $fs=isset($_GET['fs'])?str_replace('.','/',$_GET['fs']):'';
  $fs=str_replace(',','.'.($type=='j'?'js,':'css,'),$fs);
  $fs=$fs.($type=='j'?'.js':'.css');
  if($type==''||$fs==''){$is_bad_request=true;}
  //die($base);
  if($is_bad_request){header ("HTTP/1.0 503 Not Implemented");}
  $file_type=$type=='j'?'javascript':'css';
  $elements = explode(',',preg_replace('/([^?]*).*/', '\1', $fs));
  // Determine last modification date of the files
  $lastmodified = 0;
  while (list(,$element) = each($elements)) {
    $path =$base . '/' . $element;
    if (($type == 'j' && substr($path, -3) != '.js') || 
      ($type == 'c' && substr($path, -4) != '.css')) {
      header ("HTTP/1.0 403 Forbidden");
      exit;  
    }
    if (substr($path, 0, strlen($base)) != $base || !file_exists($path)) {
      header ("HTTP/1.0 404 Not Found");
      exit;
    }
    $lastmodified = max($lastmodified, filemtime($path));
  }
  // Send Etag hash
  $hash = $lastmodified . '-' . md5($fs);
  header ("Etag: \"" . $hash . "\"");
  if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && 
    stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') 
  {
    // Return visit and no modifications, so do not send anything
    header ("HTTP/1.0 304 Not Modified");
    header ("Content-Type: text/" . $file_type);
    header ('Content-Length: 0');
  } 
  else
  {
    // First time visit or files were modified
    if ($cache) 
    {
      // Determine supported compression method
      $gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
      $deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
      // Determine used compression method
      $encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none');
      // Check for buggy versions of Internet Explorer
      if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') && 
        preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
        $version = floatval($matches[1]);
        if ($version < 6)
          $encoding = 'none';
        if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1')) 
          $encoding = 'none';
      }
      // Try the cache first to see if the combined files were already generated
      $cachefile = 'cache-' . $hash . '.' . $file_type . ($encoding != 'none' ? '.' . $encoding : '');
      if (file_exists($cachedir . '/' . $cachefile)) {
        if ($fp = fopen($cachedir . '/' . $cachefile, 'rb')) {
          if ($encoding != 'none') {
            header ("Content-Encoding: " . $encoding);
          }
          header ("Content-Type: text/" . $file_type);
          header ("Content-Length: " . filesize($cachedir . '/' . $cachefile));
          fpassthru($fp);
          fclose($fp);
          exit;
        }
      }
    }
    // Get contents of the files
    $contents = '';
    reset($elements);
    while (list(,$element) = each($elements)) {
      $path = $base . '/' . $element;
      $contents .= "\n\n" . file_get_contents($path);
    }
    // Send Content-Type
    header ("Content-Type: text/" . $file_type);
    if (isset($encoding) && $encoding != 'none') 
    {
      // Send compressed contents
      $contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
      header ("Content-Encoding: " . $encoding);
      header ('Content-Length: ' . strlen($contents));
      echo $contents;
    } 
    else
    {
      // Send regular contents
      header ('Content-Length: ' . strlen($contents));
      echo $contents;
    }
    // Store cache
    if ($cache) {
      if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) {
        fwrite($fp, $contents);
        fclose($fp);
      }
    }
  }

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

相關(guān)文章

  • php生成短域名函數(shù)

    php生成短域名函數(shù)

    短網(wǎng)址流行的已經(jīng)有一段時間了,以前做新浪微博應(yīng)用的時候就有接觸,但沒有搞清楚,最近再次接觸到這個東東,仔細(xì)研究了下,發(fā)現(xiàn)短網(wǎng)址其實也挺容易的。下面就將使用php生成短網(wǎng)址的實現(xiàn)方法做一下記錄。
    2015-03-03
  • PHP Ajax實現(xiàn)無刷新附件上傳

    PHP Ajax實現(xiàn)無刷新附件上傳

    這篇文章為大家詳細(xì)主要介紹了PHP Ajax實現(xiàn)無刷新附件上傳功能的具體代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • PHP保存Base64圖片base64_decode的問題整理

    PHP保存Base64圖片base64_decode的問題整理

    在本篇文章里小編給大家整理的是關(guān)于PHP保存Base64圖片base64_decode的問題,需要的朋友們參考下。
    2019-11-11
  • [PHP]實用函數(shù)2

    [PHP]實用函數(shù)2

    [PHP]實用函數(shù)2...
    2007-11-11
  • php用戶名的密碼加密更安全的方法

    php用戶名的密碼加密更安全的方法

    在本篇文章里小編給大家整理了關(guān)于php用戶名的密碼加密更安全的方法和相關(guān)知識點,有興趣的朋友們參考下。
    2019-06-06
  • php中cookie與session的區(qū)別點總結(jié)

    php中cookie與session的區(qū)別點總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于php中cookie與session的區(qū)別點總結(jié)內(nèi)容,有興趣的朋友們可以參考學(xué)習(xí)下。
    2021-12-12
  • 解析thinkphp的左右值無限分類

    解析thinkphp的左右值無限分類

    本篇文章是對thinkphp的左右值無限分類進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • php正則preg_replace_callback函數(shù)用法實例

    php正則preg_replace_callback函數(shù)用法實例

    這篇文章主要介紹了php正則preg_replace_callback函數(shù)用法,實例分析了preg_replace_callback函數(shù)進(jìn)行正則替換的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • 不支持fsockopen但支持culr環(huán)境下下ucenter與modoer通訊問題

    不支持fsockopen但支持culr環(huán)境下下ucenter與modoer通訊問題

    網(wǎng)站上線,modoer與ucenter 下不能通訊折騰了我差不多二天,開始都以為自己的配置出問題,移植了平臺后就不能通訊了,修改了幾次配置,都沒有成功
    2011-08-08
  • php實現(xiàn)求相對時間函數(shù)

    php實現(xiàn)求相對時間函數(shù)

    這篇文章主要介紹了php實現(xiàn)求相對時間函數(shù),可實現(xiàn)簡單求相對時間為幾分鐘前或幾小時前的功能,非常簡單實用,需要的朋友可以參考下
    2015-06-06

最新評論