如何防止浏览器图像缓存? [英] How to prevent browser image caching?

查看:438
本文介绍了如何防止浏览器图像缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

防止浏览器在PHP中缓存图像的最佳方法是什么?

What is the best way to prevent the browser from caching images in PHP?

我已经尝试了标题(方法:

header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

但是,除非手动清除浏览器的缓存,否则似乎无效。

But nothings seems to work save for manually clearing the browser's cache.

我有用相同名称替换的图像,例如 avatar.png 作为用户的可更新头像,但是当它更新时,浏览器会保留旧版本。

I have images that are replaced with the same name, such as avatar.png as an updateable avatar for the user, but when it is updated, the browser holds onto an old version.

即使原件被删除并添加了新原件,浏览器仍然保留旧的 avatar.png

Even when the original is deleted and a new one is added, the browser still holds onto the old avatar.png.

有什么想法?

推荐答案

刚刚放入ar图像URL末尾的andom参数。时间戳也可以很好地用于此。

Just put a random parameter at the end of the image URL. A timestamp can also be used very well for this.

例如在PHP中:

"http://domain.com/img.png?t=" . time();

浏览器将始终将此图片加载为新图片。你应该小心使用它,它会使加载时间变慢。

The browser will always load this image new. You should use it with care though, it will make loading times slower.

这篇关于如何防止浏览器图像缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆