How can I get the value of a codeigniter session userdata variable?

Prateek Joshi picture Prateek Joshi · Mar 29, 2015 · Viewed 30.2k times · Source

I have generated the array containing session data as,

How can I echo name(durex) using the above session $userdata?

Answer

Jacob picture Jacob · Mar 29, 2015

This is the process you would follow if you wanted to set, and then retrieve, some userdata set in Codeigniter 2.0

<?php
        $user_array = array(
          'name' => 'bob',
          'email' => '[email protected]'
        );

        $this->session->set_userdata('userdata', $user_array);
        
        $user_data = $this->session->userdata('userdata');

        //Returns User's name 
        echo $user_data['name'];
?>

I'm not sure if what you pasted was formatted correctly.. also please do a print_r or var_dump($user_data) to see what it is pulling in on the $user_data variable

Edit: Now that you've updated your post, it is clear to me that what you need to do is this.

<?php
$user_data = $this->session->userdata('0');

echo $user_data->name;
?>