Logout the user from website if they meet the expi

2019-08-24 19:57发布

问题:

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.

回答1:

Please follow the below steps:

Create Common_model.php file \application\models\Common_model.php

Add common_model in autoload.php \application\config\autoload.php

$autoload['model'] = array('common_model');

Write the below code inside Common_model.php.

<?php
class Common_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
        $this->checkUserActivity();
    }
    public function checkUserActivity(){
        $this->db->where('expirytime >=',time());        
        $this->db->where('userid',$this->session->userdata('user_id'));
        $result = $this->db->get('user_sessions')->first_row(); 
        /* Check If user active within 1 minute then add 1 minute more*/
        if($result){ // Update
            $data_log = array(                
                'expirytime' => time()+(60*1)
             );
            $this->db->where('userid',$this->session->userdata('user_id'));
            $this->db->update('user_sessions', $data_log);
        } else {//Logout
            $this->activityLogout();
        }
        //echo $this->db->last_query(); exit;
    }

    public function activityLogout()
    {
        $this->session->unset_userdata('user_id'); // single unset
        $this->session->sess_destroy(); // Unset all your
        redirect('home', 'refresh');
    }
}
?>

Hope this helps you!