我想阻止所有浏览器缓存文件 script.php
我让文件 script.php 显示一个随机数:
<?php
$response = "document.write('" . rand(0,999999) . "');"; // Show random number
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
header('Content-Type', 'application/javascript');
echo $response;
和test.html:
<script src="script.php"></script>
<script src="script.php"></script>
<script src="script.php"></script>
当我在 Firefox 上打开 test.html 页面时,文件 script.php 没有被缓存,并且我得到 3 个不同的随机数。但在 IE 或 Chrome 上,我得到了相同数字的 3 倍,这意味着浏览器缓存已被使用。为什么Chrome和IE缓存不加载3次文件?如何防止这种情况发生?
最佳答案
您可以在调用脚本时添加缓存破坏器参数:
<script src="script.php?v=<?php echo mt_rand(1, 9999999) ?>"></script>
<script src="script.php?v=<?php echo mt_rand(1, 9999999) ?>"></script>
<script src="script.php?v=<?php echo mt_rand(1, 9999999) ?>"></script>
享受
关于javascript - PHP 在所有浏览器上禁用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31366077/