PHP:标头过期无效

时间:2012-10-17 13:35:09

标签: .htaccess mod-expires response-headers expires-header

我的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

3 个答案:

答案 0 :(得分: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正在覆盖.htaccess Content-Type:text/javascript规则并且PHP过期标头,因为该脚本是 .php 文件。

注释掉这个标题会在你的.htaccess中过期,看看PHP标题+ 10年到期是否仍然给出了1/1/1970的日期

答案 1 :(得分:2)

您有格式错误。

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

来自header definitions(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节中的到期计算规则。)

     

要将响应标记为“永不过期”,原始服务器会在发送响应后大约一年内发送过期日期。 HTTP / 1.1服务器不应该在将来发送超过一年的过期日期。

因此,您将来不应发送超过一年的Expires。而是指示永不过期,省略标题或使用Expires: -1

答案 2 :(得分:1)

尝试使用:

//重新验证标题

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

//然后设置过期日期

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

它解决了我的问题

相关问题