微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用PHP压缩和缓存CSS

我目前正在尝试为我的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";
        } 
    }
}
?>
您缺少< link>中的rel =“stylesheet”属性标签.除此之外,代码看起来还不错.

您可能还希望查看问题并回答here.

原文地址:https://www.jb51.cc/php/137125.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐