0

So there's this problem I'm facing all the time using sessions. I was trying to describe my exact problem, but then I've realized that it can be asked so much easier with a much simpler code.

Is there a way, to generate a random number with PHP on each page load?

I can place a simple line like echo rand(1,100); but it won't generate a random number only if I force reload the page CTRL+SHIFT+R

Now, what I have realized, is that if 10-15 seconds pass and I reload the page normally (F5), it does give me a new random number, but if I keep pressing F5, the number never changes

This is my main problem actually. I did a simple login page for a project of mine, and once the session is set and the home page is reloaded, the session is not seen or recognized, only if I force reload the page..

To explain this a bit further. I have created a file with the following content:

header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Pragma: no-cache"); // HTTP/1.0
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
echo rand(1,100);

And still, the number won't regenerate for the first 15 seconds, only if I force reload the page with CTRL+SHIFT+R.

Solved

So, after looking into this further more, testing a few things here and there, I just contacted my host to look into this. The reason was Varnish Cache. After we disabled it, everything works as expected. Thanks everyone for the suggestions and comments.

halfer
  • 19,824
  • 17
  • 99
  • 186
z3r0
  • 45
  • 8
  • Options comment points you to the correct answer, but you want to use not the accepted answer but the answer of "moderns", since the order is important. – Broco Feb 16 '17 at 12:46
  • Actually, I have tried all solutions. I've been searching for this answer for months now. No matter what headers I add to the file, the rand() won't generate a new number for 15 seconds. Same for the sessions. If I set the session and reload the page, it won't show up. If I reload the page after 15 seconds, the session is there. – z3r0 Feb 16 '17 at 12:47
  • did you try the order? Did you try different browsers? – Broco Feb 16 '17 at 13:09
  • Tried with FF, Chrome, IE, Opera. Also, tried mixing up the lines, no success. – z3r0 Feb 16 '17 at 13:15
  • You need to reset the seed generator to get truly random numbers. See [this link](http://php.net/manual/en/function.srand.php). – Nahydrin Feb 17 '17 at 04:18
  • Thanks for the answer, however, this is not the thing. The main problem is that the number won't update on reload, and the same thing happens with sessions.. I have to force reload the page to make sure the sessions are accessible. Or, it takes around 15 seconds for the sessions to be available...So, if I set a session, use header() to redirect, nothing happens. If I press CTRL+SHIFT+R, the sessions are set. Also, if I set the session, press F5 nothing happens, but if I wait about 15 seconds and press F5 again, the sessions are there. – z3r0 Feb 17 '17 at 11:10

0 Answers0