3

I need the browser to cache a large, mostly static .php file. I open it via ajax and want to add it to the current page.

After some research if found this

$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");

This works for IE, but not for chrome and firefox.

Here is the request

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control   max-age=0
Connection  keep-alive
Content-Type    application/x-www-form-urlencoded
Cookie  PHPSESSID=5dkvr42f4it8pnnnqpesj6l413
Host    localhost
Referer http://localhost/mifa/Suche.php
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1
charset utf-8

and here the response header

Cache-Control   max-age=3600
Connection  Keep-Alive
Content-Type    text/html
Date    Thu, 05 Jul 2012 15:28:22 GMT
Expires Thu, 05 Jul 2012 16:28:22 GMT
Keep-Alive  timeout=5, max=91
Pragma  cache
Server  Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
Transfer-Encoding   chunked
X-Powered-By    PHP/5.3.8

What do i need to change?

EDIT

Apparently, only IE does not append the Cache-Control max-age=0 to the request.

Here is the JS Function of the request

url = "includes/Orte.php";
obj.onreadystatechange = rState;
obj.open("GET", url, true);
obj.setRequestHeader("Pragma", "");
obj.setRequestHeader("Cache-Control", "");
obj.setRequestHeader("charset", "utf-8");
obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
obj.setRequestHeader("Connection", "close");
obj.send();

function rState(){
    if(obj.readyState == 4){
        if (obj.status == 200){
            //alert("Response Text Ajax:\n" + obj.responseText + "\nEnd Response Text");
        }
    }
}
4

2 に答える 2

2

リクエストのCache-Control: max-age=0ヘッダーは、ブラウザーにページを更新するように要求したことを意味するため、ブラウザーはキャッシュを無視します。

これを回避するには、更新を行わずにページにアクセスします (たとえば、アドレス バーにフォーカスを合わせて Enter キーを押します)。

また、ページが HTTPS URL にある場合はpublic、Cache-Control ヘッダーに追加する必要がある場合があります。追加しないと、ブラウザーによってはキャッシュされません。

于 2012-07-05T15:41:38.403 に答える
0

頭に浮かぶ 2 つのことは、最後に変更されたヘッダーと、.htaccess キャッシュ コントロールの使用です。後者は広範なタイプ用ですが、1 つのフォルダーにのみ使用でき、そのファイルはフォルダー内に単独で使用できます。

header("Last-Modified: ... ");
于 2012-07-05T15:39:26.000 に答える