Home > Back-end >  Signout not working Codeigniter Session is not getting unset
Signout not working Codeigniter Session is not getting unset

Time:12-02

I have created admin panel in codeigniter. Sign in is working fine and data is also set in session. The issue is when I click on signout it doesn't clear the session data.

My controller signout function :

public function signout()
{
    
    $this->user_m->signout();
    redirect('admin','refresh');
}

user_m model signout function :

public function signout()
{
    $adminsignindata = array('loginname','adminusername','userid','role_id','loggedin');
    $this->session->unset_userdata($adminsignindata);
}

I don't know why session is not getting unset when I use array in unset_userdata.

In localhost it is working but in live not working. In live I had to unset userdata using key wise.

CodePudding user response:

Please use the below code it will work.
Put your session in array    

$session = [
     'user_name'    => 'msrinivas',
     'name'         => 'Srinivas'
];
$this->session->set_userdata('logged_console', $session);


unset session using array key
$this->session->unset_userdata(['logged_console']);

CodePudding user response:

The CI 2.x session library contains the function unset_userdata(); which works either with a string or an associative array:

unset_userdata() can be used to remove it (the session), by passing the session key.

This function can also be passed an associative array of items to unset.

source: CI 2.x manual, Removing Session Data

below the function unset_userdata() in the session library, around line 500:

function unset_userdata($newdata = array())
{
    if (is_string($newdata))
    {
        $newdata = array($newdata => '');
    }

    if (count($newdata) > 0)
    {
        foreach ($newdata as $key => $val)
        {
            unset($this->userdata[$key]);
        }
    }

    $this->sess_write();
}

so in case, you continue with version 2.x you need to change $adminsignindata into an associative array:

$adminsignindata = array('loginname'=>'','adminusername'=>'','userid'=>'','role_id'=>'','loggedin'=>'');

in CI 3.x, this function was changed and accepts a "normal" array, as in your code example

  • Related