$_SESSION data being lost when the page refreshes

Hydrargyrum

New Member
Messages
27
Reaction score
0
Points
0
Long story short, upon logging in, the following code is executed:
$_SESSION['LoggedIn'] = 1;

When I run this on my personal server, it works perfectly. The rest of the code sees that $_SESSION['LoggedIn'] is 1; and forwards the user to a nice little member area where they can make changes to their account and whatnot.

Unfortunately, the session seems to be clearing itself when I try to run this code on x10hosting. I Googled this problem and most people recommend changing something in the php.ini file, but I obviously can't do that, so what how do I work around it?
 

morshed

New Member
Messages
4
Reaction score
0
Points
0
Long story short, upon logging in, the following code is executed:
$_SESSION['LoggedIn'] = 1;

When I run this on my personal server, it works perfectly. The rest of the code sees that $_SESSION['LoggedIn'] is 1; and forwards the user to a nice little member area where they can make changes to their account and whatnot.

Unfortunately, the session seems to be clearing itself when I try to run this code on x10hosting. I Googled this problem and most people recommend changing something in the php.ini file, but I obviously can't do that, so what how do I work around it?

Did you start a session in the first place?? You need to execute session_start() first!
 

ichwar

Community Advocate
Community Support
Messages
1,454
Reaction score
7
Points
0
Did you start a session in the first place?? You need to execute session_start() first!
yes, a lot of people make that mistake. That drove me nuts at first actually, I couldn't figure out why my sessions weren't working.:lol:
 

garrettroyce

Community Support
Community Support
Messages
5,601
Reaction score
239
Points
63
To add a little more information, there is a PHP directive set in the php config file to autostart sessions. It sounds like your other server does autostart sessions, but x10 does not.
 

ichwar

Community Advocate
Community Support
Messages
1,454
Reaction score
7
Points
0
To add a little more information, there is a PHP directive set in the php config file to autostart sessions. It sounds like your other server does autostart sessions, but x10 does not.
correct, I was not using x10.
Edit:
anyways, does php throw an error if the session is called when not yet started?
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,601
Reaction score
239
Points
63
You can check if the session is started already and if it is not, start one, like this:

Code:
if (empty(session_id())) {
     if (!session_start()) {
          exit('Session could not be started');
     }
}
 

Hydrargyrum

New Member
Messages
27
Reaction score
0
Points
0
As it turns out, I did forget to start the session. My server automatically starts sessions and x10hosting does not.
 

larosti

New Member
Messages
1
Reaction score
0
Points
0
Ok, so I'm having a very similar problem...
I have my web, open it up on localhost (my laptop), under Firefox AND IE8, and have everything running perfect with the login scripts.
Now, I uploaded it to my x10hosting, running under FF, everything's cool, but under IE, the session does not seem to prevail.
I've googled my butt off, yet the solution does not pop-up, but I found out what the problem seems to be.
Apparently, in IE, everytime a session_start() is sent, a new session is created, regardless that one was already active or not. As a consequence, the session I "log to" with $_SESSION['username'] and $_SESSION['password'], does not prevail.

I hope I explained myself correctly, so someone can help me out, 'cause I'm kinda going nuts about this.
Thanks in advance.
 

descalzo

Grim Squeaker
Community Support
Messages
9,375
Reaction score
327
Points
83
1. session_start() at the beginning of the script, before anything is sent to the browser, even a blank line.
2. Make sure your IE is accepting cookies from x10.
 
Top