I am trying to logout the user from website if they meet the expiry time which i have stored in database. I have create a table called user_sessions where i have stored current userid,logintime(varchar),expirytime(varchar) and i have added one minute extra to the login time in expiry time field so when user logged in these are all values will store in db.
So after user logged in they are redirecting to dashboard so if user doesn't do anything on site up to one minute and if they try to access next page they should logout from site.
If user keep on moving to next pages i need to add one more minute extra time in login time field everytime when they moved to next pages.
I have stored in db like this
Here is my model:
public function sessionStore($data)
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('username', $data['username']);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
$result = $query->result_array();
$recordId = $result[0]['id'];
$status = $result[0]['status'];
$sessionID = random_string('alnum',16);
$currentpass = $result[0]['password'];
if ($status != 1) {
return 4;
}
$checktrue = password_verify($data['password'], $currentpass);
// $checktrue = true;
if ($checktrue) {
$data_log = array(
'userid' => $recordId,
'sessionid' => $sessionID,
'logintime' => time(),
'expirytime' => time()+(60*1)
);
$this->db->insert('user_sessions', $data_log);
return 1;
}
}
}
Here is my controller:
public function login_user() {
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('login_view');
} else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->Login_model->login($data);
$res = $this->Login_model->sessionStore($data);
if ($result == 1) {
$userData = $this->Login_model->getUserData($data);
$sessionArray = array(
'is_logged' => TRUE,
'user_name' => $data['username'],
'first_name' => $userData['firstname'],
'last_name' => $userData['lastname'],
'userlevel' => $userData['userlevel'],
'organisation_id' => $userData['organisation_id'],
'user_id' => $userData['id'],
'lastip' => $userData['lastip']
);
$this->session->set_userdata($sessionArray);
redirect('dashboard');
} else if ($result == 2) {
$this->session->set_flashdata('message', 'Password seems to be wrong!');
$this->load->view('login_view', $data);
} else if ($result == 4) {
$this->session->set_flashdata('message', 'Username is not active!');
$this->load->view('login_view', $data);
}else {
$this->session->set_flashdata('message', 'Username not found!');
$this->load->view('login_view', $data);
}
}
}
Can anyone help me how to do that.
Thanks in advance.