CodeIgniter Forums
current user data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: current user data (/thread-74193.html)



current user data - muuucho - 08-20-2019

I like to load data from various tables that belong to a logged-in user and keep it as long as the user is logged in. I can do that in 2 ways.

1 SESSION
When a user login, I grab and store the user data in a session array, like $_SESSION['current_user']. Of course, I need to ensure that if the user data changes throughout the session, the session data has to change accordingly.

2 Grab data on every request
When a logged-in user requests any page, all user data is grabbed from each table and is stored in an array, like $current_user

Which performs best? What is best practice? Security issues?


RE: current user data - InsiteFX - 08-20-2019

Sessions is the best option.


RE: current user data - includebeer - 08-20-2019

(08-20-2019, 12:28 AM)muuucho Wrote: 1 SESSION
When a user login, I grab and store the user data in a session array, like $_SESSION['current_user']. Of course, I need to ensure that if the user data changes throughout the session, the session data has to change accordingly.

2 Grab data on every request
When a logged-in user requests any page, all user data is grabbed from each table and is stored in an array, like $current_user
3 Grab data only when you need it, and only the data you need, not everything. Do you really need all that on every request?


RE: current user data - muuucho - 08-22-2019

(08-20-2019, 03:30 AM)InsiteFX Wrote: Sessions is the best option.

OK, I actually thought that grabbing from db was a good option. 
Will rewrite some code in the near future.  Smile


RE: current user data - muuucho - 08-22-2019

(08-20-2019, 09:38 AM)includebeer Wrote:
(08-20-2019, 12:28 AM)muuucho Wrote: 1 SESSION
When a user login, I grab and store the user data in a session array, like $_SESSION['current_user']. Of course, I need to ensure that if the user data changes throughout the session, the session data has to change accordingly.

2 Grab data on every request
When a logged-in user requests any page, all user data is grabbed from each table and is stored in an array, like $current_user
3 Grab data only when you need it, and only the data you need, not everything. Do you really need all that on every request?

You are suggesting that if I don't need a lot of data for the current user, grabbing it on every request is a good option? 
Any idea when it is tipping over for sessions?


RE: current user data - includebeer - 08-23-2019

(08-22-2019, 02:24 PM)muuucho Wrote:
(08-20-2019, 09:38 AM)includebeer Wrote: 3 Grab data only when you need it, and only the data you need, not everything. Do you really need all that on every request?

You are suggesting that if I don't need a lot of data for the current user, grabbing it on every request is a good option? 
Any idea when it is tipping over for sessions?

I don't know. You didn't say how much data you load and what you're doing with it. The session is probably the best option, but my point is, why load everything just in case you need it? The session may be faster, but it uses memory. So don't load data if you don't need it. If it's only the user profile and permissions, it's fine, you will need it at every request anyway.