Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts

    Problems with session

    Sometimes I get weird problems with sessions in my development code (i.e. not live code, just the development code on my local server).

    For example, if the browser has been idle for a while, some (but not all) of the information stored in session is lost. If the browser is idle for a while longer, all the information is lost. It seems that the session information "disappears" randomly when the browser has been idle for a while (let's say 10 minutes).

    I know this all sounds rather vague and wooly, but it seems to be an intermittent error that is hard to reproduce! And the information that disappears seems to be random too.

    Any ideas as to what may be causing this?

    One thing I can think of is that I use a kind of "shorthand" way of referring to elements of the session superglobal array inside a class, as per the following example:

    PHP Code:
    class myclass
    {
      public function 
    __construct()
      {
        
    $this->m_myvar1 =& $this->m_sessionRegister("m_myvar1");
        
    $this->m_myvar2 =& $this->m_sessionRegister("m_myvar2");
      }

      public function 
    doStuff()
      {
        
    //Use m_myvar1, m_myvar2 and m_session as normal variables.  Just don't assign them by reference!
      
    }

      private function& 
    m_sessionRegister($name)
      {
        if (empty(
    $this->m_session))
          
    $this->m_session =& $_SESSION;

        if (!
    array_key_exists("registered"$_SESSION))
          
    $this->m_session["registered"] = array();

        if (!
    array_key_exists($name$_SESSION["registered"]))
          
    $this->m_session["registered"][$name] = NULL;
        
        return 
    $this->m_session["registered"][$name];
      }

      private 
    $m_myvar1;
      private 
    $m_myvar2;
      private 
    $m_session;

    I am not convinced that is the the problem though, because I see nothing wrong with my code. It works well, however when the browser has been idle for a while, perhaps m_myvar1 might be empty after having been assigned a value, whereas m_myvar2 will still contain its assigned value (for example!)
    Last edited by XmisterIS; 01-03-2012 at 11:25 AM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,093
    Thanks
    51
    Thanked 506 Times in 493 Posts
    I've been experiencing this a lot myself using UniformServer (another wamp system but less popular than xampp despite being better).

    It's one of those issues I've (wrongfully) never actually bothered to fix but from various bits I've read this is a php configuration file issue which controls the lifetime of the session. On many windows wamp setups php does a sweep of 'old' sessions whereas on linux its a bit more leniant and doesn't seem to do this.

    This is contained in my php.ini file (though I've never changed it):
    ; After this number of seconds, stored data will be seen as 'garbage' and
    ; cleaned up by the garbage collection process.
    ; http://php.net/session.gc-maxlifetime
    session.gc_maxlifetime = 1440
    That works out at 24 minutes however it doesn't state whether thats idle time or from the very start of the session being created - I guess thats an experiment needed to determine that.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    believe it or not, even i who am on shared linux hosting, have been having trouble with sessions dying alot and it taking me back to the index page on my website, Ive changed the phpini to something like maxlife of 4-5 days withouit GC yet the sessions still die after about 7-10 minutes of inactivity.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #4
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts
    I'm thinking that the only robust solution will be to set the lifetime of session to x, then store a timeout parameter equal to, for example, x/2 in a bit of javascript on the page and use a circular timer which calls back to the server using JSON and manually resets the session each time the timer gets to zero.

    A bit of a faff, I know, but not that hard to do and it will fix these problems we're having.

    But if anyone else knows of a much easier and simpler solution, I'd love to know it!

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,093
    Thanks
    51
    Thanked 506 Times in 493 Posts
    This may apply to us all:

    If different scripts have different values of session.gc_maxlifetime but share the same place for storing the session data then the script with the minimum value will be cleaning the data. In this case, use this directive together with session.save_path.
    In other words, on shared hosts whoever has the minimum setting has probably doomed the other users and on dedicated servers / wamp setups .. well still haven't tinkered with it myself yet.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #6
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by tangoforce View Post
    This may apply to us all:



    In other words, on shared hosts whoever has the minimum setting has probably doomed the other users and on dedicated servers / wamp setups .. well still haven't tinkered with it myself yet.
    in question to this post TF, is there no fix around this, ive changed my phpini setting countless times, with nothing working, however it does seem random, the fact that it sometimes logs me out after 7 minutes or so, and other times logs me out after 10 :s

    also, if i wanted to use session.save_path() to change my saved sessions directory, how would i correct my scripts to collect the sessions from that folder?
    Last edited by Dan13071992; 01-04-2012 at 03:20 PM.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,093
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Unfortunately I don't really know but I have suffered similar fates using the uniformserver on windows where sessions will randomly expire.

    I've never had chance to look into this in detail but if you can PM me in a week or so I may have more time to investigate it.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #8
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    will do, just a quick question though, is it possible to keep a session alive by using a time session variable eg:


    santex might be wrong as off the top of my head:
    PHP Code:

    /////set the session variables\\\\

    $_SESSION['timeLoggedIn'] = time();
            
    $_SESSION['uID']=$intUserID;
    //////uID is working already i know that much\\\\ 
    can we keep that $_SESSION['timeLoggedIn'] = time()+1440; so that if the time reaches that time given in the $_SESSION['timeLoggedIn'] when the user refreshes and they believe they are still logged in, the $_SESSION['timeLoggedIn'] will reset itself?

    just wondering if this is possible, if any of that makes sense to you?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,093
    Thanks
    51
    Thanked 506 Times in 493 Posts
    It makes sense and yes it will work - but only for things that your script relies on - EG if you have a "log user out after x mins" type feature for your program.

    For the session itself that php handles (IE the session file where the session array is stored) no it won't work. As I say, I may have time to investigate this next week if you can remind me.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #10
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    For shared hosting if your changes aren't working i'd first check to make sure your custom php.ini file is actually being read. You will be able to check that through phpinfo(). If your changes show up in that then it is being read so the problem may be elsewhere. If not then your account is not set up correctly for you to use your custom file or the file is not in the right path.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,093
    Thanks
    51
    Thanked 506 Times in 493 Posts
    But how does that affect wamp setups where sessions randomly get destroyed?
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #12
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by tangoforce View Post
    This may apply to us all:

    If different scripts have different values of session.gc_maxlifetime but share the same place for storing the session data then the script with the minimum value will be cleaning the data. In this case, use this directive together with session.save_path.
    In other words, on shared hosts whoever has the minimum setting has probably doomed the other users and on dedicated servers / wamp setups .. well still haven't tinkered with it myself yet.

    It wouldn't make sense for a host to set it up that way and have every account using the same directory for session data. That would be allowing one account holder to affect other account holders website which could get that host sued in a heartbeat.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #13
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by tangoforce View Post
    But how does that affect wamp setups where sessions randomly get destroyed?

    I didn't say it affected wamp setups. I was referring to shared hosting and Dans post about how he changed the max lifetime in his custom ini file and he didn't see a change.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #14
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Dan13071992 View Post
    in question to this post TF, is there no fix around this, ive changed my phpini setting countless times, with nothing working, however it does seem random, the fact that it sometimes logs me out after 7 minutes or so, and other times logs me out after 10 :s

    also, if i wanted to use session.save_path() to change my saved sessions directory, how would i correct my scripts to collect the sessions from that folder?
    You shouldn't need to change anything in your scripts. That ini file is telling PHP where to save your session data. On your shared host run a file with phpinfo() in it and you can see where the host has defaulted your session data to. You should see a line like this in the Session section

    Directive / Local Value / Master Value
    session.save_pathC:/rw_apps/wamp/tmpC:/rw_apps/wamp/tmp
    session.save_path / C:/rw_apps/wamp/tmp / C:/rw_apps/wamp/tmp

    The local value will be different if you are overrideing the master php.ini file otherwise they should be the same. Check your master value and it should hopefully be some location within your own account.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #15
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Code:
    session.save_path	/tmp	/tmp
    session.gc_maxlifetime	2592000	1440
    session.gc_divisor 	0	100
    session.gc_probability	0	1
    thats on my shared hosting, however nothing ever gets added to my /tmp folder on the server :s and as you can see i set the session.gc.maxlife to 2592000 just to test it, and played around with the other setting the see if that worked, still not keeping sessions longer than 10 minutes of being in active, and even sometimes when im active, my session will just destroy itself :s
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •