Page 1 of 1

Problem with &_SESSION When browser loads for the first time

Posted: Thu Mar 13, 2008 7:05 am
by rcb25
Hi there

I have an application that stores a random number in a session variable and uses this number to uniquely index a file. Later in the application this variable is then retrieved for that session in order to obtain the correct file that has been indexed and load that file into an applet. Now this process works fine in every instance except the very first time after the browser has been loaded. So for example if I opened the browser and ran it then the session variable would be returned as blank but if i went back and refreshed it and tried again then it would work fine.

Does anyone have an idea why it is not being set the first time after the browser is loaded?

I set the session var in a file using

$num = rand(0,10000);
session_start();
$_SESSION['num']=$num;

and then accesses it in a later file using

session_start();
$randnum = $_SESSION['num'];
$uploadedFile = "http://wired.st-andrews.ac.uk/~robin/ex ... andnum.jpg";

however as i said the first time it is run in a newly opened browser the applet tries to load http://wired.st-andrews.ac.uk/~robin/ex ... s/file.jpg with no index number attached???

Thanks for all your help

Robin

Re: Problem with &_SESSION When browser loads for the first time

Posted: Thu Mar 13, 2008 8:16 am
by Mordred
It looks like in order for what you did to work when accessed for the first time, you need to have set the session variable on the zero-eth time. :twisted:

(But then of course, it would fail on the zero-eth time, unless you also set it on the -1-st time ...)

Re: Problem with &_SESSION When browser loads for the first time

Posted: Thu Mar 13, 2008 9:23 am
by miro_igov
How about modifying your later file to:

Code: Select all

session_start();
if(!isset($_SESSION['num'])) $randnum = $_SESSION['num'] = rand(0,10000);
$uploadedFile = "http://wired.st-andrews.ac.uk/~robin/example2/uploads/file$randnum.jpg";

Re: Problem with &_SESSION When browser loads for the first time

Posted: Thu Mar 13, 2008 10:41 am
by rcb25
Thanks for the replies. Eh not too sure what you mean by your post Mordred.

miro_igov that would be a good solution however the second file needs to access the specifically indexed file uploaded in the first file so I cant just set the randnum again. I know that the session var is getting set by the first file but its just not being accessed the first time the next page loads! its so dam annoying as i cant see why it works for every other time after the first!

cheers

Robin