1
votes

I would like to prevent all browser from caching the file script.php

I have the file script.php show a random number :

<?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 :

<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. But on IE or Chrome, I got 3 times the same number that mean the browser cache has been used. Why Chrome and IE cache don't load 3 time the file? How to prevent that?

2
Have tried it with a .htaccess file? - SuperDJ
Tried but not working also - Siol

2 Answers

1
votes

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

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