Force Browser to cache images

一世执手 提交于 2020-01-02 03:53:06

问题


I am generating the images and want browser to cache them forever, but instead browser never cache them at all. This is my php code


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));

and these are the response headers which i keep getting in browser.

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

I am not setting Date, Cache-Control and Pragma but keep getting them in response.

I know i can overwrite them but honestly i don't exactly know which values to set.


回答1:


Have a look at session_cache_limiter.




回答2:


You can use:

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

You could try to overwrite Pragma too but I think these day browsers just ignore Pragma if Cache-Control is isset.

header( 'Pragma: public' );


来源:https://stackoverflow.com/questions/6214965/force-browser-to-cache-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!