CodeIgniter Forums

Full Version: How to add session data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
Quote:You can simply assign data to the 
Code:
$_SESSION
 array, as with any other variable. Or as a property of 
Code:
$this->session
.

How do you add it as a property of $this->session? I can't seem to find a relevant method anywhere and everything online seems to point to set_userdata(), but apparently, that's the 'old' method. I don't want to use the 'old' method, I want to use the new one!
$this->session is a library, 

to use it you need to load it (I prefer to load it in application/config/autoload.php), 

To load it in controller: $this->load->library('session');

for methods of session library check documentation
https://www.codeigniter.com/user_guide/l...sions.html
Thanks, however, my issue was how to add data to the session. Digging about in the class, I saw that it had a __set magic method, so have set the data by using $this->session->loggedIn = true;
if you read documentation of session library, everything you need is there, just 1 page, max it will take 10 mins of your time...
You can find how to set and get session there...
(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:
Quote:You can simply assign data to the 
Code:
$_SESSION
 array, as with any other variable. Or as a property of 
Code:
$this->session
.

How do you add it as a property of $this->session? I can't seem to find a relevant method anywhere and everything online seems to point to set_userdata(), but apparently, that's the 'old' method. I don't want to use the 'old' method, I want to use the new one!

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.