Reputation: 10548
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.
Upvotes: 4
Views: 4600
Reputation: 57268
Why don't you create a session wrapper class that handles the accessing and storing of data? This will produce much cleaner code and abstract so changes to storing methods down the line with be pretty easy.
Here's an example of a wrapper:
abstract class Session
{
private static $_started = false;
private static $_driver;
public static function start($driver = "native", $site = "default")
{
if(self::$_started === false)
{
require_once "drivers/" . $driver . ".php";
self::$_driver = new $driver($_site);
}
}
public static function set($key,$value)
{
self::$_driver->set($key,$value);
}
public static function get($key)
{
self::$_driver->get($key);
}
public static function remove($key)
{
self::$_driver->remove($key);
}
}
The above is only simple but you should get the idea, you would also have to create the native driver file that has the set of methods required, and they should store the session data accordingly.
Example of using the Session class like so:
Session::start("native");
/*
* Generic Code
*/
Session::set("key","value (:");
When fetching your "User" Object you can just do simple like so:
Session::get("userObj")->id;
Produces much cleaner code and larger scope issues.
After time goes by you can just create a new driver for storing in the database and then just change your driver from native to database.
Note: Storing objects in the database can be a little buggy especially if your code is not organized as loading the session before the user class is on scope then you can get partial objects in the session which will lead to lack of functionality.
Upvotes: 3
Reputation: 163288
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']->id
opposed 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 a user_*
key
I 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 name user_*
will be associated with a user. This is not a good way to organize objects IMO. However, when you use a user
key 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.
Upvotes: 3