Reputation: 301
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.
Upvotes: 1
Views: 94
Reputation: 38609
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
}
Upvotes: 2