问题描述
我正在设置我认为是从PHP进行缓存的正确HTTP标头,并且每秒钟发出500错误.
I'm setting what I think are the correct HTTP Headers for caching from PHP and am getting a 500 error on every second request.
有人能指出我正确的方向吗?
Can anyone point me in the right direction?
执行缓存的代码是:
<?php
header('Content-Type: text/css');
$content = file_get_contents('test.css');
header('Content-Length: '. strlen($content ));
$expires = 3600;
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
// Split the If-Modified-Since (Netscape < v6 gets this wrong)
$modifiedSince = explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
// Turn the client request If-Modified-Since into a timestamp
$modifiedSince = strtotime($modifiedSince[0]);
$current_time = time();
if (($current_time - $modifiedSince) < $expires) {
header("304 Not Modified");
//echo $current_time - $modifiedSince;
exit();
}
}
$current_time = time();
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $current_time)." GMT");
header("Expires: " . gmdate("D, d M Y H:i:s", $current_time+$expires) . " GMT");
header("Cache-Control: max-age=$expires");
echo $content;
谢谢
我很高兴地清除了缓存,有人报告说缓存正在为他们工作.这是否暗示服务器配置问题?如果重要的话,它在托管服务器上.
Edit 1: I've cleared my cache with no joy and someone reports it working for them. Does this imply a server configuration problem? It's on a hosted server if it matters.
似乎不会在IE中发生,但确实会在Firefox中发生
Edit 2: Seems not to happen in IE, but does happen in Firefox
推荐答案
当我运行您的代码时,我没有收到状态500错误,但是我看到了其他每个请求请提供文件,请提供空白页"行为与您所描述的相似.
When I ran your code I wasn't getting a Status 500 error, but I was seeing an every other request "serve the file, serve a blank page" behavior similar to what you described.
设置状态304时,您似乎没有正确使用header()函数.您丢失了"HTTP/1.1".这意味着PHP将发回Status 200标头,然后退出且没有任何输出.试试
It looks like you're not using the header() function correctly when you're setting the status 304. You're missing the "HTTP/1.1". This means that PHP is sending back a Status 200 header, and then exiting with no output. Try
header("HTTP/1.1 304 Not Modified");
如果您真的在其他所有请求中都收到Server 500错误,请在您的Web服务器(Apache?)日志中戳一下,以查看出现了哪种错误.
If you really ARE getting a Server 500 error on every other request, poke around your webserver (Apache?) logs to see what kind of errors are popping up.
这篇关于尝试从PHP使用HTTP缓存标头时出现异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!