Safari 5.1.7将标题从内容混合到页面?

时间:2012-07-18 12:36:58

标签: safari http-headers browser-cache

我将以下简单的php页面保存在我的网络服务器的根目录test.php,这是一个非常奇怪的问题:

<?
if( $_GET['img'] ) {
    header('HTTP/1.0 304 Not Modified');
    die();
} else {
    header("Cache-Control: no-store, no-cache, must-revalidate");
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<body>

<img src="/test.php?img=1">
<a href="/not_existent_page.php">Dead link</a>

</body>
</html>

因此,当页面加载时,我的服务器会响应以下标题:

Status Code 200
Cache-Control   no-store, no-cache, must-revalidate
Content-Type    text/html; charset=utf-8
Date    Wed, 18 Jul 2012 12:00:43 GMT
Server  Apache/2.2.22 (Ubuntu)
Strict-Transport-Security   max-age=172800, includeSubDomains
Vary    Accept-Encoding
x-frame-options sameorigin
x-powered-by    PHP/5.4.4-1~precise+1
x-ua-compatible IE=edge

有趣的部分是缓存标题:它说,不要缓存!

加载页面后对图片的请求有以下响应头:

Status Code 304
Date    Wed, 18 Jul 2012 12:02:20 GMT
Server  Apache/2.2.22 (Ubuntu)

它没有说缓存(但无论如何,我测试了它)。

使用firefox和chrome,网站的行为就像它应该的那样:每次重新加载页面时,都会按原样重新加载。如果我点击链接,我会收到404 apache错误。

使用safari,会发生以下情况:

如果我先打开页面,我会看到它。 当我在短时间内重新加载页面时(见下面的“短时间”意味着),总是给我一个空白的网站,当我点击它的死链接有时候给我一个空白的网站,但我在safari的web开发者控制台中看到以下标题:

Status Code 304
Connection:Keep-Alive
Date:Wed, 18 Jul 2012 12:07:41 GMT
Keep-Alive:timeout=15, max=99
Server:Apache/2.2.22 (Ubuntu)
Vary:Accept-Encoding

页面保持空白!但是:在我的apache服务器日志中,它表示它在页面重新加载时返回了状态代码200的所有优点:

// first page load
[18/Jul/2012:14:10:12 +0200] "GET /test.php HTTP/1.1" 200 979 "url/test.php" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2"

// request of picture with answer 304
[18/Jul/2012:14:10:12 +0200] "GET /test.php?img=1 HTTP/1.1" 304 266 "url/test.php" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2"

// reload within 10 seconds
[18/Jul/2012:14:10:19 +0200] "GET /test.php HTTP/1.1" 200 777 "url/test.php" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2"

(单击死链接时为404.)

现在有了一个有趣的部分:如果我等一下。加载页面10秒后,Safari会按预期运行:它只是在没有304标头的情况下重新加载页面(或在死链接上显示404)。

我对firefox和chrome没有任何问题。

所以我的问题: Safari是否会混淆页面的标题,但只有在再次加载页面/在10秒内点击链接时才会出现?我怎么能阻止这个?这是safari中的一个错误吗?

顺便说一句:如果我将标题更改为其他标题,Safari会缓存该标题。所以,如果我像这样改变test.php:

...
if( $_GET['img'] ) {
    header('HTTP/1.0 204 No Content');
    die();
} else {
....

页面重新加载现在只是中止而不做任何事情,但我在safari控制台中看到的标题是204 No Content

最后一件事:在10秒内重新加载页面会不断触发错误,而有时点击链接会在safari中运行,有时则不会。

1 个答案:

答案 0 :(得分:2)

/test.php将始终设置header("Cache-Control: no-store, no-cache, must-revalidate");标题do not cache没有任何意义,因为您明确设置了这些标题。

/test.php?img=whatever什么都不会像上面那样设置Cache-Control标头,并且将始终返回空白304 Not Modified。您在header()调用后立即退出。从技术上讲,它不是阿帕奇的形象。它的mime类型将是text / html,因此它会将你的expires / caching作为html和php(在你的.conf和/或htaccess中)。除非你做header('Content-Type: image/jpeg', true);或类似。

使用header()的第二个参数以便在某些情况下替换以前的参数也是很好的。

试试这个。如果你得到相同的结果,请告诉我

<?php
if (isset($_GET['img'])) {
    header('Content-Type: image/jpeg', true);
    header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified', true, 304);
    exit;
} else {
    header('Expires: -1', true);
    header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0', true);
}
?>