Saving user information in a session using object and regular variables

I am implementing a user authentication system for my site. I am using an open source library that supports 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 this information?

It is very difficult for me to access custom variables, because I need to first create an object to access them:

$userObj = $_SESSION['userObject'];
$userObj->userId;

instead of just accessing the user id, like this, as usual I will store the user id:

$_SESSION['userId'];

Is there any advantage to storing a bunch of user data as an object instead of just storing them as separate SESSION variables?

ps. The library also stores several variables inside the user object (identifier, username, date, email address, last db user request), but I really do not want to have all this information stored in my session, I just want to save the user ID and name user.

+3
source share
2 answers

, , , , $_SESSION['user']->id $_SESSION['user_id'], , , user_*

, , user_*, , - user_* . IMO. , user , , , .

+2

, ? , .

:

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);
     }
}

, , , , .

Session :

Session::start("native");

    /*
        * Generic Code
    */

Session::set("key","value (:");

"" :

Session::get("userObj")->id;

.

, native .

. , , , , .

+3
source

Source: https://habr.com/ru/post/1783322/


All Articles