Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have generated the array containing session data as,

array(8) {
  ["session_id"]=> string(32) "1dc5eb3730601a3447650fa4453f36cd"
  ["ip_address"]=> string(3) "::1" 
  ["user_agent"]=> string(102) "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36" 
  ["last_activity"]=> int(1427602284) 
  ["user_data"]=> string(0) "" 
  ["roll_no"]=> string(6) "111416" 
  ["is_logged_in"]=> int(1) 
  [0]=> object(stdClass)#20 (6) 
    { 
      ["name"]=> string(13) "durex" 
      ["email"]=> string(25) "[email protected]" 
      ["year"]=> string(1) "2" 
      ["semester"]=> string(1) "4" 
      ["branch"]=> string(2) "IT" 
      ["parent_email"]=> string(27) "[email protected]" 
    } 
}

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

like image 557
Prateek Joshi Avatar asked Mar 29 '15 03:03

Prateek Joshi


1 Answers

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;
?>
like image 167
Jacob Avatar answered Nov 05 '22 06:11

Jacob