PHP的缓存控制和过期标头 [英] Cache control and expires header for PHP

查看:124
本文介绍了PHP的缓存控制和过期标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置我的标题

$offset = 60 * 15;

header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
header("Cache-Control: max-age=$offset, must-revalidate"); 

然而,当运行FireBug时,它会给我以下标题信息

However when running FireBug its giving me the following header information

HTTP/1.1 200 OK
Date: Mon, 25 Jul 2011 12:15:12 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9
X-Powered-By: PHP/5.2.9
Expires: Sat, 01 Jan 2000 00:00:01 GMT
Cache-Control: post-check=0, pre-check=0, max-age=0
Pragma: no-cache
Last-Modified: Mon, 25 Jul 2011 12:15:13 GMT
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

任何人都知道为什么我的标题不被识别?

Anyone know why my headers aren't being recognised?

推荐答案

我知道几个版本之前,Firebug有问题,所有请求都是200而不是304.这是我的代码,我是用于css:

I know that few versions ago, Firebug had problems and all requests were 200 instead 304. Here is my code, that i'm using for css:

    <?php
    if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
      $if_modified_since = preg_replace('/;.*$/', '',   $_SERVER['HTTP_IF_MODIFIED_SINCE']);
    } else {
      $if_modified_since = '';
    }

    $mtime = filemtime($_SERVER['SCRIPT_FILENAME']);
    $gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';

    if ($if_modified_since == $gmdate_mod) {
      header("HTTP/1.0 304 Not Modified");
      exit;
    }

    header("Last-Modified: $gmdate_mod");
    header('Content-type: text/css');

    header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (60*60*24*45)) . ' GMT');
    // rest of the code
    ?>

这篇关于PHP的缓存控制和过期标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆