简体   繁体   English

PHP动态文件缓存问题

[英]PHP Dynamic Files Caching Problem

I have some CSS and JS files that are dynamically generated by PHP using this in my .htaccess: 我有一些CSS和JS文件,这些文件是由PHP使用.htaccess文件中的动态生成的:

AddHandler application/x-httpd-php .css .js

And then inside the files, for example: 然后在文件内部,例如:

<?PHP if ($Browser == 'msie') { ?>
.bind('selectstart', function(event) { [...] })
<?PHP } ?>

On the top of them (first line ) i use a conditional get based on Last-Modified header, like this: 在它们的顶部(第一行),我使用基于Last-Modified标头的条件获取,如下所示:

<?PHP if (FileIsCached(getlastmod())) die(); ?>

This is the function: 这是功能:

public static function FileIsCached($Timestamp)
{
    $LastModified = substr(date('r', $Timestamp), 0, -5).'GMT';
    $IfModifiedSince = (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);

    if (($Semicolon = strrpos($IfModifiedSince, ';')) !== false)
        $IfModifiedSince = substr($IfModifiedSince, 0, $Semicolon);

    header('Last-Modified: '.$LastModified);

    if (!$IfModifiedSince || ($IfModifiedSince != $LastModified))
        return false;

    header('HTTP/1.1 304 Not Modified');

    return true;
}

I don't need to check encoding because I use automatic gzipping inside my php.ini: 我不需要检查编码,因为我在php.ini中使用了自动gziping:

 output_handler = ob_gzhandler

I put them normally inside my index.php like this: 我通常将它们放在我的index.php中,如下所示:

<script type="<?PHP echo $JavascriptMIME; ?>" src="/script.js"></script>

Everything works like a charm... this is my first load response header: 一切都像一个魅力……这是我的第一个负载响应标头:

HTTP/1.1 200 OK
Date: Fri, 16 Sep 2011 21:21:51 GMT
Last-Modified: Fri, 16 Sep 2011 19:25:37 GMT
Content-Encoding: gzip
Vary: Accept-Encoding
Cache-Control: max-age=31536000, public
Expires: Sat, 15 Sep 2012 21:21:51 GMT
Content-Type: application/javascript
Content-Length: 6161
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive

This is my cached response header: 这是我的缓存响应标头:

HTTP/1.1 304 Not Modified
Date: Fri, 16 Sep 2011 21:24:39 GMT
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Expires: Sat, 15 Sep 2012 21:24:39 GMT
Cache-Control: max-age=31536000, public
Vary: Accept-Encoding

The problem is that sometimes HTTP data seems to get messed up, maybe by gzipped content or something similar. 问题在于,有时HTTP数据似乎被弄乱了,可能是因为压缩了内容或类似内容。 Chrome network console sometimes show the following error: "Resource interpreted as Other but transferred with MIME type undefined". Chrome网络控制台有时会显示以下错误:“资源解释为其他,但使用MIME类型未定义传输”。 Mozilla sometimes makes 2 requests for index.php or try to download it with download prompt. Mozilla有时会对index.php发出2个请求,或尝试通过下载提示进行下载。 Sometimes file requests stop at 5 when I normally load 7 files and the last downloaded file content is messed up like: 有时,当我通常加载7个文件并且最近下载的文件内容混乱时,文件请求在5处停止:

���������ÿÿ���������HTTP/1.1 200 OK
Date: Fri, 16 Sep 2011 19:53:26 GMT
Server: Apache/2.2.17 (Win32) PHP/5.3.6
X-Powered-By: PHP/5.3.6
Cache-Control: must-revalidate, no-cache, no-store, private, public
Cache-Control: max-age=0, max-stale=0, post-check=0, pre-check=0
Expires: Wed, 11 Apr 1984 18:36:00 GMT
Expires: 0
Last-Modified: Fri, 16 Sep 2011 19:53:26 GMT
Pragma: no-cache
Content-Encoding: gzip
Vary: Accept-Encoding
Content-Type: application/javascript
Content-Length: 674
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
....

What can it be? 会是什么

Try turning the output handler off in PHP and see if things work correctly. 尝试在PHP中关闭输出处理程序,看看一切是否正常。

I found this comment on the php manual page that is probably what you are running into. 我在php手册页上发现了此注释 ,这可能是您遇到的问题。

You could try that and if it is the problem, use Apache's SetOutputFilter to gzip the content for you by adding the following to httpd.conf instead of having PHP doing it 您可以尝试一下,如果有问题,可以使用Apache的SetOutputFilter通过将以下内容添加到httpd.conf中,而不是使用PHP来为您的内容gzip压缩

<Location />
# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</Location>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM