无法在codeigniter中更改会话值[重复]

无法在codeigniter中更改会话值[重复]

问题描述:

This question already has an answer here:

I use the session in codeigniter, it works well but I can't change the value. I use this function :

$this->session->set_userdata('check', 0);

I must change the value of 'check' to 0.

Why can't ?

My question is, why i can't change the value session ? And I set the value like this for the first time :

$sess_array = array(
        'userid' => $row->userid,
        'pseudo' => $row->pseudo,
        'check'  => $row->check
);  
$this->session->set_userdata('logged_in', $sess_array);
</div>

此问题已经存在 这里有一个答案: p>

  • Codeigniter修改会话数据 3 answers span> li> \ r ul> div>

    我在codeigniter中使用会话,它运行良好但我无法更改值。 我使用此函数: p >

      $ this-&gt; session-&gt; set_userdata('check',0); 
      code>  pre> 
     
     

    我必须更改 'check'的值为0. p>

    为什么不能? p>

    我的问题是,为什么我无法更改值会话? 我第一次设置这样的值: p>

      $ sess_array = array(
    'userid'=&gt; $ row-&gt; userid,
    'pseud  '=&gt; $ row-&gt; pseudo,
    'check'=&gt; $ row-&gt; check 
    );  
     $ this-&gt; session-&gt; set_userdata('logged_in',$ sess_array); 
      code>  pre> 
      div>

$sess_array = array(
    'userid' => $row->userid,
    'pseudo' => $row->pseudo,
    'check'  => $row->check
); 
// you have set session key 'logged_in' 
$this->session->set_userdata('logged_in', $sess_array);

// Get session value from existing key
$arrSession = $this->session->userdata('logged_in');
$arrSession['check'] = 0;
$this->session->set_userdata('logged_in', $arrSession);

You need to update value as shown above.

If you wish to set 'check' value as you indicated. you need to do as following

$sess_array = array(
    'userid' => $row->userid,
    'pseudo' => $row->pseudo,
    'check'  => $row->check
); 
// Note that no key specified. 
$this->session->set_userdata($sess_array);
// Now i think you can set check value. Give it a try. Let me know which works.
$this->session->set_userdata('check',0);

Can you try unsetting the value and then assign a new value to it, Something like the below code:

$check= $this->session->userdata('check');

if(!empty($check)){
  $this->session->unset_userdata('check');
  $this->session->set_userdata('check', 0);
}
echo $this->session->userdata('check');

Let me know if that works!!