9

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) "durex@gmail.com" 
      ["year"]=> string(1) "2" 
      ["semester"]=> string(1) "4" 
      ["branch"]=> string(2) "IT" 
      ["parent_email"]=> string(27) "parent@durex.com" 
    } 
}

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

Jacob
  • 1,483
  • 8
  • 13
Prateek Joshi
  • 3,929
  • 3
  • 41
  • 51
  • you can loop the array using php's foreach(); and get the desired output. more info on foreach in php's documentation website http://in3.php.net/manual/en/control-structures.foreach.php – Ghostman Mar 29 '15 at 04:09

4 Answers4

17

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' => 'bob@bob.com'
        );

        $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;
?>
Jacob
  • 1,483
  • 8
  • 13
  • It displays nothing . It is not even displaying any error though. Can you suggest me a way to add more values to userdata session after fetching more data from database. – Prateek Joshi Mar 29 '15 at 04:06
  • Could you please do a `var_dump($this->session->all_userdata())` and update your original post with what the output of that is exactly? I think your original post has some errors. – Jacob Mar 29 '15 at 04:08
  • I also just updated my post to walk you through the entire process of setting and getting user data – Jacob Mar 29 '15 at 04:15
  • session->userdata('0'); echo $user_data->name; ?> It worked. Thanks a ton :D – Prateek Joshi Mar 29 '15 at 04:22
  • Yes. You can see that it is an object in the array output that I formatted for you. You must have done `$this->session->set_userdata(0, $usersdata)` – Jacob Mar 29 '15 at 04:23
  • How can I print roll_no .Can you explain me how you are selecting the indexes '0' and then printing the values. – Prateek Joshi Mar 31 '15 at 01:06
  • Sorry I don't quite understand. Could you rephrase that? – Jacob Mar 31 '15 at 03:37
1

Please check if this works. If you are getting it from the session values:

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

if you are getting if from your array:

<?php

        echo $your_array_name['user_data'][0]['name'];
?>

Note: This is the correct format of the array in PHP

<?php
$my_array = Array ( 
  'session_id' => '8f9286115a38df9ed54a65b135d4e8c0',
  'ip_address' => '::1',
  'user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36',
  'last_activity' => 1427600580,
  'roll_no' => 111416, 
  'is_logged_in' => 1,
  'email' => '',
  'user_data' => Array ( 
     0 => Array ( 
       'name' => 'durex', 
       'email' => 'durex@gmail.com', 
       'year' => 2 ,
       'semester' => 4, 
       'branch' => 'CSE', 
       'parent_email' => 'parent@durex.com'
     )
  )
);
echo $my_array['user_data'][0]['name'];
?>
Arzgethalm
  • 516
  • 5
  • 14
1

1.get your session and save in variable $usernya = $this->session->get_userdata('sess');

2.Now you can show the data with simple echo echo $usernya['username'];

Hans
  • 61
  • 10
0

In Codeigniter 3 you can retrieve session data by simply using PHP's native $_SESSION variable

$_SESSION['item']

Or through the magic getter

$this->session->item

It still allows for the traditional

$this->session->userdata('item');

More in the docs

digout
  • 4,041
  • 1
  • 31
  • 38