【问题标题】:Force Browser to cache images强制浏览器缓存图像
【发布时间】:2011-06-02 13:13:09
【问题描述】:

我正在生成图像并希望浏览器永远缓存它们,但浏览器根本不会缓存它们。 这是我的php代码


header('Content-type:'.$file->content_type);
header('Expires: Thu, 15 Apr '.(date('Y')+10).' 20:00:00 GMT');
header('Content-Disposition: attachment; filename="'.$file->file_name.'"');
echo(file_get_contents($model_files->files_path.$file->md5.$model_files->seprator.$file->file_name));

这些是我不断在浏览器中获取的响应标头。

Date:            Thu, 02 Jun 2011 12:56:56 GMT
Server:          Apache mod_fcgid/2.3.6 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By     PHP/5.2.15
Cache-Control    no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma           no-cache
Expires          Thu, 15 Apr 2021 20:00:00 GMT
Content-Length   321823
Keep-Alive       timeout=5, max=100
Connection       Keep-Alive
Content-Type     image/png

我没有设置 Date、Cache-Control 和 Pragma,但会不断得到它们的响应。

我知道我可以覆盖它们,但老实说我不知道​​要设置哪些值。

【问题讨论】:

  • 你找到答案了吗?

标签: php caching http-headers browser


【解决方案1】:

【讨论】:

  • +1 如果您在此页面上使用 session_start,这可能是导致缓存标头的原因。如果您不需要会话,只需删除 session_start。如果您不使用它,请在您的问题中提及它,您需要寻找这些标头的其他原因。
【解决方案2】:

你可以使用:

header( 'Cache-Control: max-age=37739520, public' );

你也可以尝试覆盖 Pragma,但我认为如果 Cache-Control isset,现在的浏览器会忽略 Pragma。

header( 'Pragma: public' );

【讨论】:

    猜你喜欢
    • 2010-11-20
    • 2011-10-08
    • 2010-09-12
    • 1970-01-01
    • 2011-10-11
    • 2020-12-19
    • 2016-02-18
    相关资源
    最近更新 更多