472,096 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Storing session info in Local Storage?

2 2Bits
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_duration = 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.
Oct 25 '21 #1
4 12321
Niheel
2,452 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2.   $cookie_name = 'sessionid';
  3.   $value = uniqid(time());
  4.   $expire = time() + 60*60*24*3; // 3 days from now
  5.   $path = '/blog';
  6.   $domain = 'www.mysite.com';
  7.   $secure = isset($_SERVER['HTTPS']); // or use true/false
  8.   $httponly = true;
  9.  setcookie($cookie_name, $value, $expire, $path, $domain, $secure, $httponly);
  10.  
if you want to you can also set the PHP configuration to use cookies for sessions:
Expand|Select|Wrap|Line Numbers
  1. session.use_only_cookies = 1
  2. session.cookie_lifetime = 0  // '0' = expire when browser closes
  3. session.cookie_secure = 1
  4. session.cookie_httponly = 1
  5.  
more information in the php manual
https://www.php.net/manual/en/features.cookies.php
Oct 25 '21 #2
dev7060
624 Expert 512MB
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_duration = 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.
Oct 25 '21 #3
Dormilich
8,658 Expert Mod 8TB
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.
Oct 26 '21 #4
mopufo
2 2Bits
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2.   $cookie_name = 'sessionid';
  3.   $value = uniqid(time());
  4.   $expire = time() + 60*60*24*3; // 3 days from now
  5.   $path = '/blog';
  6.   $domain = 'www.mysite.com';
  7.   $secure = isset($_SERVER['HTTPS']); // or use true/false
  8.   $httponly = true;
  9.  setcookie($cookie_name, $value, $expire, $path, $domain, $secure, $httponly);
  10.  
Expand|Select|Wrap|Line Numbers
  1. session.use_only_cookies = 1
  2. session.cookie_lifetime = 0  // '0' = expire when browser closes
  3. session.cookie_secure = 1
  4. session.cookie_httponly = 1
  5.  
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.
Nov 4 '21 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Celebrate | last post: by
reply views Thread by Mirek Rewak | last post: by
3 posts views Thread by Astha | last post: by
3 posts views Thread by Atmapuri | last post: by
reply views Thread by keyser soze | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.