I had created a dynamic web page. On the main page there are three sliding images. I created another page to change that main page image (sliding). So after I submit that second page, I wrote a PHP code to go back to main page. But the images are not changing because my browser has that web page in its cache. If I remove cache manually, or restart the browser it is working. How I remove my browser cache in PHP coding? give me a solution. Thanks.
我创建了一个动态网页。在主页面上有三个滑动图像。我创建了另一个页面来更改主页面图像(滑动)。所以在我提交第二页后,我编写了一个PHP代码返回主页面。但是图像没有变化,因为我的浏览器在其缓存中有该网页。如果我手动删除缓存,或重新启动浏览器它正在运行。我如何删除PHP编码中的浏览器缓存?给我一个解决方案。谢谢。
6 个解决方案
#1
You can't clear the cache, however you can ask the page not to cache in the first place:
您无法清除缓存,但您可以要求页面首先不缓存:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
This has already been asked on *, and this answer was taken from there, purely for the purpose of giving credit to the fellow user who has already answered this question.
已经在*上询问了这个问题,这个答案来自那里,纯粹是为了给已经回答了这个问题的同行用户。
The previous question is here: How to clear browser cache with php?
上一个问题在这里:如何用php清除浏览器缓存?
Or, as already said, you can use META tags to do it, but you asked for a PHP solution, so here it is :D
或者,如前所述,您可以使用META标记来执行此操作,但您要求使用PHP解决方案,因此它是:D
best of luck.
祝你好运。
#2
You may also add a random string to the image-URI:
您还可以向image-URI添加随机字符串:
<img src="myimage.png?r=12345" alt="">
If the random string varies every time, the main page is reloaded (!), the browser will retrieve the image from the server.
如果随机字符串每次都变化,则重新加载主页面(!),浏览器将从服务器检索图像。
#3
Try using the HTML cache control META TAGS:
尝试使用HTML缓存控件META TAGS:
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
or use EXPIRES:
或使用EXPIRES:
<META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 02 May 2015 21:00:00 GMT">
#4
Or you disable your cache with PHP by the header(); function, ore you give a refferance (imgurl.com/imagename.jpg?r=1234) to your image. I would recommend the second method. Why? Because if you shut down the cache for the whole page using the header function op php, it will just slow down your site. So give a random string to your images.
或者您通过header()禁用PHP缓存;功能,矿石你给你的图像一个refferance(imgurl.com/imagename.jpg?r=1234)。我会推荐第二种方法。为什么?因为如果你使用标题函数op php关闭整个页面的缓存,它只会减慢你的网站速度。所以给你的图像一个随机字符串。
#5
There is one trick that can be used.The trick is to append a parameter/string to the file name in the script tag and change it when you file changes.
有一个技巧可以使用。技巧是将参数/字符串附加到脚本标记中的文件名,并在提交更改时更改它。
<script src="myfile.js?version=1.0.0"></script>
The browser interprets the whole string as the file path even though what comes after the "?" are parameters. So wat happens now is that next time when you update your file just change the number in the script tag on your website (Example <script src="myfile.js?version=1.0.1"></script>
) and each users browser will see the file has changed and grab a new copy.
浏览器将整个字符串解释为文件路径,即使在“?”之后出现了什么。是参数。因此,现在发生的情况是,下次更新文件时,只需更改网站脚本标记中的数字(示例
#6
If your files are often changing, in your place, I would simply send the following header with PHP along with them:
如果您的文件经常更改,在您的位置,我只需使用PHP发送以下标头:
header("Cache-Control: no-cache, must-revalidate");
You can add these lines just before the first issue of your PHP script into your pages. With this, you tell the browser, that it should not cache the page and it should request the contents every time again.
您可以在第一次发布PHP脚本之前将这些行添加到页面中。有了这个,你告诉浏览器,它不应该缓存页面,它应该每次都请求内容。
For pictures, you can write the following line into your HTACCESS:
对于图片,您可以在HTACCESS中写下以下行:
<FilesMatch "\.(jpg|png|gif)$">
Header set Cache-Control "no-cache, must-revalidate"
</FilesMatch>
#1
You can't clear the cache, however you can ask the page not to cache in the first place:
您无法清除缓存,但您可以要求页面首先不缓存:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
This has already been asked on *, and this answer was taken from there, purely for the purpose of giving credit to the fellow user who has already answered this question.
已经在*上询问了这个问题,这个答案来自那里,纯粹是为了给已经回答了这个问题的同行用户。
The previous question is here: How to clear browser cache with php?
上一个问题在这里:如何用php清除浏览器缓存?
Or, as already said, you can use META tags to do it, but you asked for a PHP solution, so here it is :D
或者,如前所述,您可以使用META标记来执行此操作,但您要求使用PHP解决方案,因此它是:D
best of luck.
祝你好运。
#2
You may also add a random string to the image-URI:
您还可以向image-URI添加随机字符串:
<img src="myimage.png?r=12345" alt="">
If the random string varies every time, the main page is reloaded (!), the browser will retrieve the image from the server.
如果随机字符串每次都变化,则重新加载主页面(!),浏览器将从服务器检索图像。
#3
Try using the HTML cache control META TAGS:
尝试使用HTML缓存控件META TAGS:
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
or use EXPIRES:
或使用EXPIRES:
<META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 02 May 2015 21:00:00 GMT">
#4
Or you disable your cache with PHP by the header(); function, ore you give a refferance (imgurl.com/imagename.jpg?r=1234) to your image. I would recommend the second method. Why? Because if you shut down the cache for the whole page using the header function op php, it will just slow down your site. So give a random string to your images.
或者您通过header()禁用PHP缓存;功能,矿石你给你的图像一个refferance(imgurl.com/imagename.jpg?r=1234)。我会推荐第二种方法。为什么?因为如果你使用标题函数op php关闭整个页面的缓存,它只会减慢你的网站速度。所以给你的图像一个随机字符串。
#5
There is one trick that can be used.The trick is to append a parameter/string to the file name in the script tag and change it when you file changes.
有一个技巧可以使用。技巧是将参数/字符串附加到脚本标记中的文件名,并在提交更改时更改它。
<script src="myfile.js?version=1.0.0"></script>
The browser interprets the whole string as the file path even though what comes after the "?" are parameters. So wat happens now is that next time when you update your file just change the number in the script tag on your website (Example <script src="myfile.js?version=1.0.1"></script>
) and each users browser will see the file has changed and grab a new copy.
浏览器将整个字符串解释为文件路径,即使在“?”之后出现了什么。是参数。因此,现在发生的情况是,下次更新文件时,只需更改网站脚本标记中的数字(示例
#6
If your files are often changing, in your place, I would simply send the following header with PHP along with them:
如果您的文件经常更改,在您的位置,我只需使用PHP发送以下标头:
header("Cache-Control: no-cache, must-revalidate");
You can add these lines just before the first issue of your PHP script into your pages. With this, you tell the browser, that it should not cache the page and it should request the contents every time again.
您可以在第一次发布PHP脚本之前将这些行添加到页面中。有了这个,你告诉浏览器,它不应该缓存页面,它应该每次都请求内容。
For pictures, you can write the following line into your HTACCESS:
对于图片,您可以在HTACCESS中写下以下行:
<FilesMatch "\.(jpg|png|gif)$">
Header set Cache-Control "no-cache, must-revalidate"
</FilesMatch>