7

我会在 codeigniter 中设置一个会话值

$sess_array = array(
'id' => $row->Login_Id,
'username' => $row->Login_Name,
'postid'=> $row->Fk_Post_Id,
 );
$CI->session->set_userdata('logged_in', $sess_array);

然后如何检索特定的(比如 id)会话值。我试过这些

$createdby=$this->session->userdata('id');
$createdby=$this->session->userdata($logged_in['id']);

但失败了。

4

7 回答 7

12

为此使用数组语法。喜欢,

$this->session->userdata['logged_in']['id'];
于 2013-04-15T08:55:29.723 回答
5

像这样设置

$sess_array = array(
                    'id'        =>  $row->Login_Id,
                    'username'  =>  $row->Login_Name,
                    'postid'    =>  $row->Fk_Post_Id,
                );

$CI->session->set_userdata($sess_array);

检索

$createdby  =   $this->session->userdata('id');
于 2013-04-15T08:52:30.060 回答
3
$sess_array = array(
'id' => $row->Login_Id,
'username' => $row->Login_Name,
'postid'=> $row->Fk_Post_Id,
 );
$CI->session->set_userdata('logged_in', $sess_array);

然后

$logged_in = $this->session->userdata('logged_in');
$createdby = $logged_in['id'];
于 2013-04-15T09:05:43.773 回答
3
$createdby = $this->session->userdata(logged_in['id']);

或者

$createdby = echo $_SESSION[logged_in['id']];
于 2013-04-15T08:56:13.227 回答
3
Session write process. session content could be dynamic value
$logged_in = [
'id'=> 1,
'name'=> 'test'
]; 

Particular Session retrieve process.


    $this->session->userdata['logged_in']['id'];
于 2015-04-17T11:55:50.650 回答
2
$pdata = array(
                'id'        =>  $row->Login_Id,
                'username'  =>  $row->Login_Name,
               );  // pass ur data in the array

$this->session->set_userdata('session_data', $pdata); //Sets the session


$pdata = $this->session->userdata('session_data'); //Retrive ur session

$pdata['id'] will give u the corresponding id 
于 2013-04-15T09:02:23.297 回答
1

在较新的版本中,您可以简单地使用

$this->session->username
$this->session->id
$this->session->postid
于 2017-12-17T03:49:30.463 回答