从网站注销用户是否满足到期时间从笨数据库(Logout the user from website

2019-10-28 15:22发布

我想,如果他们满足我已经存储在数据库中的过期时间注销从网站的用户。 我已创建在那里我存储的实际用户ID,logintime(VARCHAR),expirytime(VARCHAR)的表称为user_sessions和我1分钟添加额外的到,所以当用户登录这些都是值将存储在到期时间字段中的登录时间D b。

因此,用户登录后,在他们被重定向到仪表板,所以如果用户没有在现场做任何一分钟,如果他们试图进入下一个页面,他们应该从网站注销。

如果用户继续移动到下一个页面,我需要在登录时间字段,每次增加一个额外分钟时他们搬到下一个页面。

我已经存储在数据库这样的

这里是我的模型:

  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;
        } 
    } 
} 

这里是我的控制器:

 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);
        }
    }
}

谁能帮我该怎么做。

提前致谢。

Answer 1:

请按照下面的步骤:

创建Common_model.php文件\application\models\Common_model.php

添加common_model在autoload.php \application\config\autoload.php

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

写里面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');
    }
}
?>

希望这可以帮助你!



文章来源: Logout the user from website if they meet the expiry time from database in codeigniter