php 如何用php清除浏览器缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1037249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear browser cache with php?
提问by ZA.
How to clear browser cache with php?
如何用php清除浏览器缓存?
回答by ZA.
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type: application/xml; charset=utf-8");
回答by user1032289
You can delete the browser cache by setting these headers:
您可以通过设置这些标头来删除浏览器缓存:
<?php
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
回答by Marcelo Agimóvel
It seams you need to versionate, so when some change happens browser will catch something new and user won't need to clear browser's cache.
它接缝你需要版本化,所以当发生一些变化时,浏览器会捕捉到一些新的东西,用户不需要清除浏览器的缓存。
You can do it by subfolders (example /css/v1/style.css)or by filename (example: css/style_v1.css)or even by setting different folders for your website, example:
您可以通过子文件夹(example /css/v1/style.css)或文件名(example: css/style_v1.css)甚至为您的网站设置不同的文件夹来完成,例如:
www.mywebsite.com/site1
www.mywebsite.com/site2
www.mywebsite.com/site3
And use a .htaccess or even change httpd.conf to redirect to your current application.
并使用 .htaccess 甚至更改 httpd.conf 来重定向到您当前的应用程序。
If's about oneimage or page:
如果是关于一个图像或页面:
<?$time = date("H:i:s");?>
<img src="myfile.jpg?time=<?$time;?>">
You can use $time on parts when you don't wanna cache. So it will always pull a new image. Versionate it seams a better approach, otherwise it can overload your server. Remember, browser's cache it's not only good to user experience, but also for your server.
当您不想缓存时,您可以在部件上使用 $time。所以它总是会拉一个新的图像。版本化它是一种更好的方法,否则它会使您的服务器过载。请记住,浏览器的缓存不仅对用户体验有好处,而且对您的服务器也有好处。

