PHP:header过期时间无效

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

为什么要使用 .htaccess 标签?你将把这段代码放在哪里? - Dave
我的 .htaccess 将我的资源设置为 +10 年。所以我只是在模仿它。缓存已清除并且我已经验证了我得到了 200 OK。date 功能正常。 - StackOverflowNewbie
你能分享一下你的mod_expires和.htaccess配置吗? - Dave
你能把你的htaccess文件内容放在这里吗? - undone
https://raw.github.com/kiphughes/.htaccess/master/.htaccess - StackOverflowNewbie
显示剩余4条评论
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正在覆盖.htaccess中的Content-Type:text/javascript规则和PHP过期标头,因为该脚本是一个.php文件。

在你的.htaccess中注释掉这个过期标头,看看PHP标头+10年的过期时间是否仍然会给出1970年1月1日的日期。


我刚刚启用了mod_headers并将此规则添加到我的htaccess文件中,它确实超过了php Expires头。 - WebChemist
对我没用。我正在使用CodeIgniter,所以我的页面实际上没有“.php”扩展名。浏览器将资源识别为text/javascript。你是如何测试的?你使用了常规的PHP文件吗? - StackOverflowNewbie
是的,我复制了你提供的代码片段,验证了它给出了正确的未来日期,然后我看到了你的.htaccess文件链接,我添加了filesmatch规则,结果还是显示1970年1月1日。我刚刚测试了一个URL重写,以删除.php扩展名,但仍然显示1970年的头部日期,因此在您的URL中没有.php扩展名并不意味着它不匹配该规则...您的文件夹中是否有其他htaccess文件? - WebChemist
尝试这个以确保。进入httpd.conf,注释掉LoadModule mod_headers行,保存,重新启动Apache并再次检查。这应该绝对要么排除mod_headers的问题,要么告诉你它确实是问题所在。 - WebChemist
谢谢您的赏金。不过您没有回复,我的最后一条评论有帮助解决您的问题吗?如果您的服务器还有其他问题,我想知道,以防我自己遇到类似的问题... - WebChemist
还没有机会去看一下。我会在有时间的时候更新。谢谢! - StackOverflowNewbie

2

您的格式存在错误。

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

来自头部定义 (14.21):

An example of its use is

 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 clients and caches MUST treat other invalid date formats, especially including the value "0", as in the past (i.e., "already expired").

To mark a response as "already expired," an origin server sends an Expires date that is equal to the Date header value. (See the rules for expiration calculations in section 13.2.4.)

To mark a response as "never expires," an origin server sends an Expires date approximately one year from the time the response is sent. HTTP/1.1 servers SHOULD NOT send Expires dates more than one year in the future.

因此,您不应将过期时间设置为超过一年。如果要表示永不过期,请省略标头或使用Expires: -1。请注意保留HTML标记。

Steven Sauders指出,这个1年的限制只是一个建议。雅虎将其设置为10年,我想。不过,我对Expires: -1很感兴趣,因为我看到谷歌在使用它。 - StackOverflowNewbie
header('Expires: -1');header_remove('Expires'); 都没有起作用。响应头仍然显示 Expires:Thu, 01 Jan 1970 00:00:00 GMT - StackOverflowNewbie
@StackOverflowNewbie 看起来服务器被强制设置了这个,尝试使用缓存控制和最大有效期作为一种解决方法。 - Zaffy

1

尝试使用:

// 再次验证标头

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

// 然后设置过期日期

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

之前它解决了我的问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接