1

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?

3
  • Have tried it with a .htaccess file? Commented Jul 12, 2015 at 9:34
  • Tried but not working also Commented Jul 12, 2015 at 18:06
  • possible duplicate of Disable cache for dynamic created Javascript Commented Jul 13, 2015 at 22:53

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I forget to tell but i can't use random number in the url. The integration code <script src="script.php"></script> canot change.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.