How to add session data |
(07-17-2018, 12:51 PM)chillifish Wrote: This may sound like a ridiculous question, but I'm trying to work out how to add data to the session (to indicate a user has logged in). The documentation here says: What isn't completely clear is that, in terms of user data, the session class is in many ways simply an abstraction layer around the superglobal $_SESSION. Keep digging into the source code and you'll find that the session class property $userdata is primarily a reference to the superglobal $_SESSION. As the documentation says, the two "recommended" ways to set session user data are: PHP Code: $_SESSION['loggedIn'] = TRUE; or PHP Code: $this->session->loggedIn = TRUE; The second example uses the magic method __set($key, $value). Look closely and you will see that it executes the code in my first example above (where $key==='loggedIn' and $value===TRUE). That tells me that directly assigning values to $_SESSION is the way to go when setting user data. The session class magic method __get($key) is the compliment to __set($key, $value) returning $_SESSION[$key] if the key exists and NULL if does not. That NULL business is handy because you should be checking the key exists whenever you access an array by an explicit key anyway. So, for me, using something like PHP Code: if($this->session->loggedIn){ seems an optimal way to read and use session data. The "old" session function set_userdata($data, $value) is convenient when you want to set multiple $_SESSION items with a single call. Don't feel like you should avoid using it just because it is a "legacy" method. |
Messages In This Thread |
How to add session data - by chillifish - 07-17-2018, 12:51 PM
RE: How to add session data - by neuron - 07-17-2018, 10:52 PM
RE: How to add session data - by chillifish - 07-18-2018, 02:52 AM
RE: How to add session data - by neuron - 07-18-2018, 11:07 PM
RE: How to add session data - by dave friend - 07-23-2018, 02:14 PM
|