我如何取消设置指定的会话

时间:2016-05-24 06:37:52

标签: codeigniter session codeigniter-2

我想知道我是如何取消指定会话的。

$array = array(
                'Name' => 'Jhon',
                'Age' => '19',
                'Remark' => 'Tests'
            );
$this->session->set_userdata('registrasi',$array);

我知道我可以用

删除会话
$this->session->unset_userdata('registrasi');

如何删除Remark?所以最后的结果会是这样的。

 'Name' => 'Jhon',
 'Age' => '19'

2 个答案:

答案 0 :(得分:1)

您可以使用unset_userdata()

$this->session->unset_userdata()

在你的情况下

$this->session->unset_userdata('Remark');

我厌倦了这个我自己的例子。请查看以下

这是我的会话

$session = array(
    'id' => $result[0]['id'],
    'username'  => $name,
    'logged_in' => TRUE
);
$this->session->set_userdata($session);

打印会话

print_r($this->session->all_userdata());

<强>输出

Array ( 
    [session_id] => 4cc6794ab4d1ee062e377945c92148dc 
    [ip_address] => ::1 
    [user_agent] => Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36 
    [last_activity] => 1464104585 
    [user_data] => 
        [id] => 1 
        [username] => Admin 
        [logged_in] => 1 
        [flash:new:success] => Welcome Admin 
) 

即将从上述会话中移除 username

$this->session->unset_userdata('username');

并打印回来

print_r($this->session->all_userdata());

<强>输出

Array ( 
    [session_id] => 4cc6794ab4d1ee062e377945c92148dc 
    [ip_address] => ::1 
    [user_agent] => Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36 
    [last_activity] => 1464104585 
    [user_data] => 
        [id] => 1 
        [logged_in] => 1 
        [flash:new:success] => Welcome Admin 
    )
  
    

因此经过测试并运作良好

  

答案 1 :(得分:0)

也许你可以尝试一下。

public function index()
{

    $array = array(
        'Name' => 'Jhon',
        'Age' => '19',
        'Remark' => 'Tests'
    );
    $this->session->set_userdata('registrasi',$array);

    print_r($this->session->all_userdata());
}

public function delete()
{

    $i = "Tests";

    $del = $this->session->userdata('registrasi');
    $index = array_search($i, $del);
    unset($del[$index]);
    $this->session->set_userdata('registrasi', $del);
    print_r($this->session->all_userdata());
}