8

私は現在、CSSファイルの圧縮方法を実現しようとしています。基本的にJSファイルと同じようにコピーしていますが、機能していません。Firebugツールで確認しましたが、CSSが読み込まれていません。

css.php圧縮されたCSSファイルを呼び出すを呼び出すにはどうすればよいですか?

JSでコードを操作すると、ファイルは次のようになりますscripts.php(拡張子は指定しませんでした.js)。

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

CSSファイルにも同じことをしたかったのです。

<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";
        } 
    }
}
?>
4

2 に答える 2

3

rel="stylesheet"タグに属性がありません<link>。それを除けば、コードは問題ないように見えます。

ここで質問と回答を確認することもできます。

于 2012-08-06T20:01:03.700 に答える
1

縮小してみてください。このクラスは、jsとcssを圧縮してキャッシュします。

于 2012-08-06T20:24:04.233 に答える