Storing session info in Local Storage?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mopufo
    New Member
    • Oct 2021
    • 2

    #1

    Storing session info in Local Storage?

    My website has a back-end admin area used by volunteers to add content to the Db which contains over 60 table. When each volunteer logs into the Admin area I store their Username and access lever in the $_SESSION associative array. This information is used in a log record when a record is updated.

    At the beginning of each Admin module I test for the existence of a Session and if not started, then one is started. On the most used module I also set the timeout_duratio n = 21600 (6 hours).

    The problem is that the volunteers will oftentimes work a couple of hours and then do some research, have lunch, take a nap, go to bed while the browser is still open and the admin page is still active. Thus, when they return to the computer, they again start working with the Admin module and when a record is updated the Username is Unknown. Unknown because the Session has expired and thus, the Session variable is not set and the Username is set to Unknown.

    So the question is... Is there anyway to solve the problem using Sessions or do I need to just store the info in Local Storage?

    Thanks in advance.
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2433

    #2
    The short answer is set a cookie (bits of data stored on the client by the browser).
    Store the session id information in a cookie and this will allow you to let the session persist.

    How to set a cookie in PHP:
    Code:
      $cookie_name = 'sessionid';
      $value = uniqid(time());
      $expire = time() + 60*60*24*3; // 3 days from now
      $path = '/blog';
      $domain = 'www.mysite.com';
      $secure = isset($_SERVER['HTTPS']); // or use true/false
      $httponly = true;
     setcookie($cookie_name, $value, $expire, $path, $domain, $secure, $httponly);
    if you want to you can also set the PHP configuration to use cookies for sessions:
    Code:
    session.use_only_cookies = 1
    session.cookie_lifetime = 0  // '0' = expire when browser closes
    session.cookie_secure = 1
    session.cookie_httponly = 1
    more information in the php manual
    niheel @ bytes

    Comment

    • dev7060
      Recognized Expert Contributor
      • Mar 2017
      • 656

      #3
      My website has a back-end admin area used by volunteers to add content to the Db which contains over 60 table. When each volunteer logs into the Admin area I store their Username and access lever in the $_SESSION associative array. This information is used in a log record when a record is updated.

      At the beginning of each Admin module I test for the existence of a Session and if not started, then one is started. On the most used module I also set the timeout_duratio n = 21600 (6 hours).

      The problem is that the volunteers will oftentimes work a couple of hours and then do some research, have lunch, take a nap, go to bed while the browser is still open and the admin page is still active. Thus, when they return to the computer, they again start working with the Admin module and when a record is updated the Username is Unknown. Unknown because the Session has expired and thus, the Session variable is not set and the Username is set to Unknown.

      So the question is... Is there anyway to solve the problem using Sessions or do I need to just store the info in Local Storage?
      Assuming many things here. Show the relevant code.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Is there anyway to solve the problem using Sessions
        The problem here is that you have two different ways to start a session (namely with and without username). To solve that you need to require authentication whenever a session does not exist. This way you'll always have a username available.

        Comment

        • mopufo
          New Member
          • Oct 2021
          • 2

          #5
          The short answer is set a cookie (bits of data stored on the client by the browser).
          Store the session id information in a cookie and this will allow you to let the session persist.

          How to set a cookie in PHP:

          Code:
           
            $cookie_name = 'sessionid';
            $value = uniqid(time());
            $expire = time() + 60*60*24*3; // 3 days from now
            $path = '/blog';
            $domain = 'www.mysite.com';
            $secure = isset($_SERVER['HTTPS']); // or use true/false
            $httponly = true;
           setcookie($cookie_name, $value, $expire, $path, $domain, $secure, $httponly);
          Code:
          session.use_only_cookies = 1
          session.cookie_lifetime = 0  // '0' = expire when browser closes
          session.cookie_secure = 1
          session.cookie_httponly = 1
          more information in the php manual.
          https://www.php.net/manual/en/features.cookies.php
          I solved my problem using the example you gave, thank you all very much for your help.
          Last edited by mopufo; Nov 4 '21, 10:27 PM.

          Comment

          Working...