I’m in the process of implementing a user authentication system for my website. I’m using an open source library that maintains user information by creating a User object and storing that object inside my php SESSION variable. Is this the best way to store and access that information?
I find it a bit of a hassle to access the user variables because I have to create an object to access them first:
$userObj = $_SESSION['userObject'];
$userObj->userId;
instead of just accessing the user id like this how I would usually store the user ID:
$_SESSION['userId'];
Is there an advantage to storing a bunch of user data as an object instead of just storing them as individual SESSION variables?
ps – The library also seems to store a handful of variables inside the user object (id, username, date joined, email, last user db query) but I really don’t care to have all that information stored in my session. I only really want to keep the user id and username.
I think that logically grouping related data together under one key is best, it couples the relevant data with a common parent, it also gives you more wiggle room with regards to key names, so you could have
$_SESSION['user']->idopposed to$_SESSION['user_id'], allows you to have property name context, so you don’t have to provide the context in the key name as you would with auser_*keyI also think that there is a bigger concept that comes into play here, when you use
user_*you are pretty much saying that anything with the key nameuser_*will be associated with a user. This is not a good way to organize objects IMO. However, when you use auserkey and stick all of the associated data underneath it so to speak, then you have a much cleaner top level and a real nested data hierarchy as opposed to a linear one.