7

我的PHP代码:

$expires_date = date('D, j F Y H:i:s', strtotime('now + 10 years')) . ' GMT';           
header("Expires: $expires_date");
header('Content-type: text/javascript');

echo 'hello world';

当我检查响应标头时,我看到:

Expires:Thu, 01 Jan 1970 00:00:00 GMT

我究竟做错了什么?

更新:

只是在试验,但似乎我什至无法通过header_remove('Expires');. 我仍然看到 1970 年的日期。

更新:

我的回复标题:

Cache-Control:private
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:74
Content-Type:text/javascript
Date:Wed, 17 Oct 2012 22:40:45 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Keep-Alive:timeout=5, max=98
Server:Apache/2.2.21 (Win32) PHP/5.3.9
Vary:Accept-Encoding
X-Powered-By:PHP/5.3.9
4

3 回答 3

5

查看您的 htaccess 文件:

<FilesMatch "\.(htm|html|php)$">
        Header set Expires "Thu, 01 Jan 1970 00:00:00 GMT"

        # TODO: Google.com's setting are the following
        # Expires -1
        # Cache-Control private, max-age=0
</FilesMatch>

看起来您FilesMatch .php正在覆盖 .htaccessContent-Type:text/javascript规则,并且 PHP expires 标头因为脚本是一个.php文件。

在你的 .htaccess 中注释掉这个 header expires,看看 PHP header +10 year expires 是否仍然给出 1970 年 1 月 1 日的日期

于 2012-10-26T06:32:12.267 回答
2

您有格式错误。

  • 使用d代替j
  • 使用M代替F
  • 使用gmdate()代替date()

标题定义(14.21):

它的一个使用例子是

 Expires: Thu, 01 Dec 1994 16:00:00 GMT

 Note: if a response includes a Cache-Control field with the max-
 age directive (see section 14.9.3), that directive overrides the
 Expires field.

HTTP/1.1 客户端和缓存必须像过去一样对待其他无效的日期格式,特别是包括值“0”(即“已经过期”)。

为了将响应标记为“已经过期”,源服务器发送一个等于 Date 标头值的 Expires 日期。(请参阅第 13.2.4 节中的到期计算规则。)

要将响应标记为“永不过期”,源服务器会在响应发送后大约一年发送 Expires 日期。HTTP/1.1 服务器不应该在未来一年以上发送 Expires 日期。

所以你不应该在未来发送超过一年的 Expires。而是指示永不过期省略标头或使用Expires: -1.

于 2012-10-28T00:06:37.763 回答
1

尝试使用:

// 再次重新验证标头

header("Cache-Control: no-cache, must-revalidate");

// 然后设置过期日期

header("Expires: Sat, 26 Jul 2011 05:20:00 GMT"); // Date to expire

它以前解决了我的问题

于 2012-11-01T09:20:25.323 回答