1

Hi guys i am trying to use logout code in my codigniter so due to some reason its not working properly.

Here is my logout code:

 public function Logout() {
    $this->Login_model->saveLogout();
    $this->session->sess_destroy();
    redirect('Login');
}

Here is my model code:

    public function saveLogout() {
    $recordId=$this->session->userdata('user_id');
     $ipaddress = $this->input->ip_address();
    $dataLog = array(
        'userid' => $recordId,
        'entertime' => time(),
        'ip' => $ipaddress,
        'log_operation' => 'Logout'
    );
    $dataUser = array(
        'previous_visit' => time(),
        'lastip' => $ipaddress
    );
    $this->db->insert('log_table', $dataLog);
    $this->db->set($dataUser); //value that used to update column  
    $this->db->where('id', $recordId); //which row want to upgrade  
    $this->db->update('users');  //table name
}

When i clicked on logout it is showing this below error

    Column 'userid' cannot be null

INSERT INTO `log_table` (`userid`, `entertime`, `ip`, `log_operation`) VALUES (NULL, 1534252667, '::1', 'Logout')

Can anyone help me what is the problem

Thanks in advance.

user200
  • 291
  • 1
  • 4
  • 21

1 Answers1

2

When you write $this->session->sess_destroy(); without any condition check write after other, it will destroy the session before execute other. Same like headers already sent

Do like this

In Controller

public function Logout() {
    $return = $this->Login_model->saveLogout(); # alter

    if(!$return)
    {
        echo "went wong"; die;
    }
    else
    {
        $this->session->sess_destroy();
        redirect('Login');
    }
}

In Model

public function saveLogout() {

    # your model code

    return true; # add this
} 
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • can you please check this question and help me https://stackoverflow.com/questions/51924399/get-td-values-display-as-static-in-codeigniter?noredirect=1#comment90798509_51924399 – user200 Aug 20 '18 at 06:18
  • i am trying from past few days i didn't get any idea of how to do can you please help me – user200 Aug 20 '18 at 06:19
  • i have one doubt may i ask – user200 Sep 01 '18 at 12:49