Reputation: 1372
I am including the same files multiple times on the same page. I noticed when I do this, it seems PHP is caching the files. When I generate a random number with the rand function, it is the same in both includes. Anyone know how I can stop PHP from doing this? I tried some different header functions like this but they don't work:
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Pragma: no-cache"); // HTTP/1.0
Edit: Ok, I'm dumb and I forgot that I was calling the includes from a jquery script which I believe is caching the files. I went back to see the code some one was asking for and noticed it. Thanks everyone.
Upvotes: 1
Views: 1846
Reputation: 168655
I would suggest that including the same file multiple times is not particularly good practice.
A better solution would be to include it once, and call the functions it contains multiple times.
Obviously, this will mean changing the way the code works -- I guess it's currently written as a block of code that is run as soon as it it included. You'd need to change it so that it's enclosed in a function (or several functions, as required) so that it can be called at will.
Then just include it once at the start of your program.
I know this doesn't directly answer the question, but doing it this way is better coding practice, and will make your code much easier to manage and maintain.
Hope that helps.
Upvotes: 1
Reputation: 324610
Chances are you're seeding the pRNG with the same value within that file. Headers affect the browser, not the script anyway.
I know for certain that included files are not cached: I made a console program where every "function" was implemented by including a file. Changing the file without restarting the program still allowed it to load the updated files.
So, your problem is either the random seed, or something elsewhere. Try echo microtime()
to see if it gives the same result both times.
Upvotes: 0