0

我有一个非常奇怪的问题,以下简单的 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 中有效,有时则无效。

4

1 回答 1

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() 调用后立即退出。从技术上讲,它不是 apache 的图像。它的 mime 类型将是 text/html,因此它会将您的过期/缓存作为 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);
}
?>
于 2012-07-26T20:38:21.313 回答