使用PHP-文本编辑器压缩和使用JS文件的mergin

Cod*_*s90 10 javascript php caching

我目前正在努力缩小JS文件以提高页面速度.我已经能够找到最简单的方法,几乎​​所有的js文件,除了两个.问题是我试图在我的网站上实现的wmd编辑器的js文件.js文件wmd.jsshowdown.js没有被函数压缩和缓存scripts.php.我用firebug工具检查过,在scripts.php最终压缩的js文件中都没有包含任何文件的响应头.

我压缩这些js文件(wmd&showdown)并将其合并为一个的过程有什么问题?示例网站

js/scripts.php-负责js文件的压缩和缓存

<?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/javascript; charset: UTF-8");

ob_start ("ob_gzhandler");

foreach (explode(",", $_GET['load']) as $value) {
    if (is_file("$value.js")) {
        $real_path = mb_strtolower(realpath("$value.js"));
        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.js"));
            include("$value.js");echo "\n";
        } 
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

我调用compressed.js的方式

<script type = "text/javascript" src = "js/scripts.php?build=12345&load=wmd,showdown"></script>
Run Code Online (Sandbox Code Playgroud)

小智 6

我想问题是声明include("$value.js");echo "\n";
这样,如果包含的javascript文件至少包含一个" <? "字符串,并且如果你在"php.ini"配置文件中启用了" short_open_tag "选项,则部分是javascript代码正在从PHP解释器中解析,好像它是PHP代码,可能会抛出语法错误,因此忽略了后续的包含.
看看"prettify.js"源代码,我看到有效地出现了"<?" 在串线471也许改变线include("$value.js");echo "\n";readfile("$value.js");echo "\n";应该解决的问题.