Welcome Guest, Not a member yet? Register   Sign In
saving a user's $_SESSION vars
#1

(This post was last modified: 06-30-2021, 07:09 AM by richb201.)

Is there a way to save a specific user's session variables, so that next time they log in, I can load them up again? Can I keep them packaged up in a "bag" in their record in MYSQL?
proof that an old dog can learn new tricks
Reply
#2

@richb201 ,

There are definitely multiple ways you could go about saving a users session variables.

Here is an example of one way. You could create a field on the user table that keeps a copy of the session variable (whenever they logoff). Then when the user logs in, you can have the system check if any session variables were save. If so, then set the new session variables with the previous session variables.
Reply
#3

(This post was last modified: 06-30-2021, 12:21 PM by richb201.)

(06-30-2021, 08:11 AM)php_rocs Wrote: @richb201 ,

There are definitely multiple ways you could go about saving a users session variables.

Here is an example of one way.  You could create a field on the user table that keeps a copy of the session variable (whenever they logoff). Then when the user logs in, you can have the system check if any session variables were save.  If so, then set the new session variables with the previous session variables.

Yes, exactly, but there are like 20 of them. Is there anyway to save the whole bunch? And BTW, what happens of they click the X in the corner of the browser window?
proof that an old dog can learn new tricks
Reply
#4

(This post was last modified: 06-30-2021, 04:50 PM by Gary.)

(06-30-2021, 12:19 PM)richb201 Wrote:
(06-30-2021, 08:11 AM)php_rocs Wrote: @richb201 ,

There are definitely multiple ways you could go about saving a users session variables.

Here is an example of one way.  You could create a field on the user table that keeps a copy of the session variable (whenever they logoff). Then when the user logs in, you can have the system check if any session variables were save.  If so, then set the new session variables with the previous session variables.

Yes, exactly, but there are like 20 of them. Is there anyway to save the whole bunch? And BTW, what happens of they click the X in the corner of the browser window?

I've not tried this, but perhaps make a copy of the user's session cookie (from the session directory on the drive to another directory for safe-keeping, perhaps using the user_id as a filename, or, if you wanted to make it complicated, generate and store a filename in the database)... and then simply restore as much of what you wanted/needed from the stored cookie (if such a cookie exists for that user_id) and then delete the stored cookie (?). This way, if the session variables ever change, or are big, it has no impact on one's databases- in terms of storage space or requiring structural/field changes.
Reply
#5

@richb201 ,

When you say 20 of them are you referring to 20 session variables? If so, you could simply put them in an array and store the array.
Reply
#6

Save the session array into an encrypted cookie just like how the login remember me works.

An yes the cookie will save an array.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(06-30-2021, 08:07 PM)php_rocs Wrote: @richb201 ,

When you say 20 of them are you referring to 20 session variables?  If so, you could simply put them in an array and store the array.

This would also be my approach. e.g. I would probably have a $_SESSION['user']['settings'] variable.

Use json_encode() to store it in a user_settings table.
Use json_decode() to update the $_SESSION['user']['settings'] array.
The save frequency (successful login, user initiated, per page request, per session settings update etc.) is up to you.
Reply
#8

Well, in thinking about this I have about 35 session var's. Some pertain to this users setup. But some pertain to userids and passwords. And some temporarily hold tokens received from a few supporting applications. So I surely don't want to send them all up to a cookie. I have some code that loads the few session vars that I need in certain places in the app. I decided to create a function that I keep in MyModel so that I can reload those two arrays back into $_SESSION on demand.

I am having another problem associated with this section which deals with counting the results of an sql query. I will post that query immediatley.
proof that an old dog can learn new tricks
Reply
#9

Then you need to save them to the database, you could save it to the database using a json string.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#10

(07-05-2021, 02:22 AM)InsiteFX Wrote: Then you need to save them to the database, you could save it to the database using a json string.

Thanks Insite. I could surely save them as a json string and even save that to s3. But when i bring it back, and then unjsonize it, how do I get it back into the $_SESSION? 
proof that an old dog can learn new tricks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB