2012-08-06 6 views
8

현재 CSS 파일에 압축 방법을 적용하려고합니다. 기본적으로 JS 파일에 대해 동일한 방법으로 복사하고 있지만 작동하지 않습니다. Firebug 도구로 확인했지만 CSS가로드되지 않았습니다.PHP로 CSS 압축 및 캐싱

css.php을 어떻게 호출하여 압축 된 CSS 파일을 호출 할 수 있습니까?

<script type="text/javascript" src="js/scripts.php?build=123&load=foo,bar"></script> 

내가 내 CSS 파일에 대해 동일한 작업을 수행하고 싶었 : JS와 코드 작업

, 파일은 (내가 .js 확장자를 지정하지 않은) scripts.php입니다

<link href="styles/css.php?build=123&load=foo,bar/jquery-ui-1.8rc2.custom" type="text/css"> 

css.php 압축을 수행해야합니다 :

<?php 
error_reporting(E_ERROR); 
// see http://web.archive.org/web/20071211140719/http://www.w3.org/2005/MWI/BPWG/techs/CachingWithPhp 
// $lastModifiedDate must be a GMT Unix Timestamp 
// You can use gmmktime(...) to get such a timestamp 
// getlastmod() also provides this kind of timestamp for the last 
// modification date of the PHP file itself 

function cacheHeaders($lastModifiedDate) { 
    if ($lastModifiedDate) { 
     if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModifiedDate) { 
      if (php_sapi_name()=='CGI') { 
       Header("Status: 304 Not Modified"); 
      } else { 
       Header("HTTP/1.0 304 Not Modified"); 
      } 
      exit; 
     } else { 
      $gmtDate = gmdate("D, d M Y H:i:s \G\M\T",$lastModifiedDate); 
      header('Last-Modified: '.$gmtDate); 
     } 
    } 
} 

// This function uses a static variable to track the most recent 
// last modification time 
function lastModificationTime($time=0) { 
    static $last_mod ; 
    if (!isset($last_mod) || $time > $last_mod) { 
     $last_mod = $time ; 
    } 
    return $last_mod ; 
} 

lastModificationTime(filemtime(__FILE__)); 
cacheHeaders(lastModificationTime()); 
header("Content-type: text/css; charset: UTF-8"); 

ob_start ("ob_gzhandler"); 

foreach (explode(",", $_GET['load']) as $value) { 
    if (is_file("$value.css")) { 
     $real_path = mb_strtolower(realpath("$value.css")); 
     if (strpos($real_path, mb_strtolower(dirname(__FILE__))) !== false ||strpos($real_path, mb_strtolower(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR)) !== false) { 
      lastModificationTime(filemtime("$value.css")); 
      include("$value.css"); echo "\n"; 
     } 
    } 
} 
?> 

답변

3

<link> 태그의 rel="stylesheet" 속성이 누락되었습니다. 그 외에도 코드는 괜찮아 보입니다.

질문과 대답 here을보고 싶을 수도 있습니다.

+1

와우, 그런 중요한 세부 사항을 놓쳤습니다. 고맙습니다! – CodingWonders90

1

시도 minify. 이 클래스는 js와 css를 압축하고 캐시합니다.