$_SESSION data being lost when the page refreshes

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by Hydrargyrum, May 30, 2009.

  1. Hydrargyrum

    Hydrargyrum New Member

    Messages:
    27
    Likes Received:
    0
    Trophy 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?
     
  2. morshed

    morshed New Member

    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Did you start a session in the first place?? You need to execute session_start() first!
     
  3. ichwar

    ichwar Community Advocate Community Support

    Messages:
    1,454
    Likes Received:
    7
    Trophy Points:
    0
    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:
     
  4. garrettroyce

    garrettroyce Generally Helpful Member Community Support

    Messages:
    4,079
    Likes Received:
    131
    Trophy 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.
     
  5. ichwar

    ichwar Community Advocate Community Support

    Messages:
    1,454
    Likes Received:
    7
    Trophy Points:
    0
    correct, I was not using x10.
    Edit:
    anyways, does php throw an error if the session is called when not yet started?
     
    Last edited: May 30, 2009
  6. garrettroyce

    garrettroyce Generally Helpful Member Community Support

    Messages:
    4,079
    Likes Received:
    131
    Trophy 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');
         }
    }
     
  7. Hydrargyrum

    Hydrargyrum New Member

    Messages:
    27
    Likes Received:
    0
    Trophy Points:
    0
    As it turns out, I did forget to start the session. My server automatically starts sessions and x10hosting does not.
     
  8. larosti

    larosti New Member

    Messages:
    1
    Likes Received:
    0
    Trophy 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.
     
  9. descalzo

    descalzo Grim Squeaker Community Support

    Messages:
    9,375
    Likes Received:
    327
    Trophy 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.
     

Share This Page