Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Safari 5.1.7将标题从内容混合到页面?_Safari_Http Headers_Browser Cache - Fatal编程技术网

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

Safari 5.1.7将标题从内容混合到页面?,safari,http-headers,browser-cache,Safari,Http Headers,Browser Cache,以下简单的php页面保存在我的Web服务器的根目录下,作为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 xmln

以下简单的php页面保存在我的Web服务器的根目录下,作为
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 304
Date    Wed, 18 Jul 2012 12:02:20 GMT
Server  Apache/2.2.22 (Ubuntu)
它没有提到缓存(但不管怎样,这并不重要,我已经测试过了)

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

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

如果我先打开这一页,我就会看到它。 当我在短时间内重新加载页面时(请参见下文“短时间”的含义),它总是给我一个空白站点,当我单击和死链接时,它有时会给我一个空白站点,但我在safari的web developer控制台中看到以下标题:

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中的一个bug吗

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

...
if( $_GET['img'] ) {
    header('HTTP/1.0 204 No Content');
    die();
} else {
....
页面重新加载现在只是中止而不做任何事情,但是我在safari控制台中看到的标题是
204无内容


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

/test.php将始终设置
标题(“缓存控制:无存储,无缓存,必须重新验证”)
标题中的
不缓存
没有什么有趣的,因为您正在显式设置这些标题

/test.php?img=无论如何都不会像上面那样设置缓存控制头,并且总是返回空白304 Not Modified。您将在header()调用之后立即退出。从技术上讲,它不是apache的映像。它的mime类型将是text/html,因此它将作为html和php(在.conf和/或htaccess中)携带expires/caching。除非您使用
标题('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);
}
?>

修复它的是
内容类型:image/jpeg
。我不知道为什么只有safari 5.1.7才需要这样做,看起来它无法处理缺少的
内容类型
标题,并且会把事情搞混。因为我的服务器从未为
304
请求发送任何
内容类型
(我发布了问题中返回的唯一标题)。Safari 6已经修复了这个问题。
<?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);
}
?>