在Codeigniter中输出会话数据

How do I output the current session data.

This is my code in the controller

$this->load->library('session'); //load library of session; encryption key = key
$data['user'] = $this->session->set_userdata('email', 'email@hp.com');

The value that should be outputed in the view is email@hp.com I tried this in the view.php but it is not working.

foreach($user as $row){
 $row->email;
 }

but it doesnt work. it says invalid argument.

Try like this,

$this->session->set_userdata('email', 'email@hp.com');  // set the session
$data['user'] = $this->session->userdata('email');   // get the session and store it in an array which is passed to the view

and in view file echo the value,

echo $user;

Otherwise, you can directly get the session value in view file like,

$objCI =& get_instance(); //now CI object can be used
$objCI->session->userdata('email'); 

set_userdata() is used to set data in session, to get session data you should use userdata()

like

$this->session->set_userdata('email', 'email@hp.com');//to set data
$data['user']=$this->session->userdata('email');//to get sessiondata

And in view echo $user; will do.

here