简体   繁体   English

PHP在所有浏览器上禁用缓存

[英]PHP disable cache on all browser

I would like to prevent all browser from caching the file script.php 我想防止所有浏览器缓存文件script.php

I have the file script.php show a random number : 我有文件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;

and test.html : test.html

<script src="script.php"></script>
<script src="script.php"></script>
<script src="script.php"></script>

When i open test.html page on Firefox, the file script.php is not cached and i get 3 differents random number. 当我在Firefox上打开test.html页面时,文件cache.php没有被缓存,并且我得到了3个不同的随机数。 But on IE or Chrome, I got 3 times the same number that mean the browser cache has been used. 但是在IE或Chrome上,我得到的数字是使用浏览器缓存的3倍。 Why Chrome and IE cache don't load 3 time the file? 为什么Chrome和IE缓存无法加载3倍的文件? How to prevent that? 如何预防呢?

You can add a cache buster parameter when calling the 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>
<script src="script.php?v=<?php echo mt_rand(1, 9999999) ?>"></script>

Enjoy 请享用

Try this 尝试这个

<?php
   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");
?>

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

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