简体   繁体   English

如何删除浏览器缓存?

[英]How to remove the browser cache?

I have one image website where users can vote images. 我有一个图片网站,用户可以在其中投票图片。 IMAGES ARE RANDOMLY GENERATED ON FIRST PAGE! 图片是在第一页上随机生成的! Once they vote they're redirected using window.location to the image details page. 他们投票后,将使用window.location将其重定向到图像详细信息页面。 If they click back they will see the same image...from the browser cache..and they can vote it unlimited times.... 如果他们单击返回,他们将在浏览器缓存中看到相同的图像..并且他们可以无限制地投票...。

How to I remove the cache? 如何删除缓存? I want the first page to refresh when I click back button! 当我单击“后退”按钮时,我希望第一页刷新! I already used : 我已经用过:

<meta http-equiv="Pragma" content="no-cache">
    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <META HTTP-EQUIV="Expires" CONTENT="-1">

and

<input type="hidden" id="refreshed" value="no">
<script type="text/javascript">
    onload=function(){
        var e=document.getElementById("refreshed");
        if(e.value=="no")e.value="yes";
        else{e.value="no";location.reload();}
        }
</script>

thanks !!!!! 谢谢 !!!!!

You don't want to use client-side checking to prevent users voting multiple times. 您不想使用客户端检查来防止用户多次投票。 That's just silly. 那真是愚蠢。

You want to check on the server whether a user has already voted for that image, and if so, direct them to some "Oops you've already voted for that" page and don't count the vote. 您想在服务器上检查用户是否已经对该图像进行了投票,如果是,请将其定向到某些“您已经对该图像进行了投票的糟糕”页面,并且不计算投票数。

Using location.replace("URL_HERE"); 使用location.replace("URL_HERE"); instead of location.href = "URL_HERE"; 而不是location.href = "URL_HERE"; will prevent the redirect from creating a new entry in the user's history. 将阻止重定向在用户历史记录中创建新条目。 But I still think Anon's answer is absolutely correct. 但是我仍然认为Anon的答案是绝对正确的。

And what if the "hacker" has disabled JS ? 如果“黑客”禁用了JS,该怎么办?

I would recommend for you to do few other things: 我建议您做一些其他事情:

  • limit the amount of image one can vote for per 24 hours 限制每24小时可以投票的图片数量
  • keep all the votes in DB for 24 hours 将所有投票保留在DB中24小时
  • give registered users higer max-votes limit 给注册用户更大的最大投票限制

You need to feed the image to the user dynamically instead of pointing to a file in a directory. 您需要动态地将图像提供给用户,而不是指向目录中的文件。 So you have an 所以你有一个

image.php?id=1234 

inside image.php you pretty much just open the image, uudecode it, and print it to the browser. 在image.php内部,您几乎只需打开图像,对它进行uudecode,然后将其打印到浏览器即可。 Since the image is retrieved programatically you can simply block it or force a new image on them. 由于图像是通过程序检索的,因此您可以简单地将其阻止或在其上施加新图像。 If you don't want users to retrieve a specific image and want to always serve a random image, then don't use IDs at all in the url and simply serve up a random image file. 如果您不希望用户检索特定图像并希望始终提供随机图像,则不要在网址中完全使用ID,而只需提供随机图像文件即可。

It also helps to stick in useless random data in the url, in this case generating a UUID would be a good idea. 它还有助于将无用的随机数据保留在url中,在这种情况下,生成UUID将是一个好主意。 So even if you go with a solution that just does then do something like 因此,即使您采用的解决方案只是做到了,

print '<img src="'. $image .'?'. uniqid() .'">';

It's no silver bullet, but it's another trick that prevents image caching. 这不是灵丹妙药,但这是防止图像缓存的另一个技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM