如果用户满足codeigniter中数据库的过期时间,则从网站注销用户

bvuwiixz  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(262)

我试图注销用户从网站,如果他们满足到期时间,我已经在数据库中存储。我创建了一个名为user\u sessions的表,其中存储了当前userid、logintime(varchar)、expirytime(varchar),并在expiry time字段中添加了一分钟的额外登录时间,因此当用户登录时,这些值都将存储在db中。
所以,用户登录后,他们重定向到 Jmeter 板,所以如果用户不做任何事在网站上一分钟,如果他们试图访问下一页,他们应该从网站注销。
如果用户继续移动到下一页,我需要在登录时间字段中添加一分钟额外的时间,每当他们移动到下一页。
我已经像这样存储在数据库中了
这是我的模型:

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

有人能帮我怎么做吗。
提前谢谢。

2cmtqfgy

2cmtqfgy1#

请按照以下步骤操作:
创建common_model.php文件 \application\models\Common_model.php 在autoload.php中添加公共\u模型 \application\config\autoload.php ```
$autoload['model'] = array('common_model');

在common_model.php中编写以下代码。
希望这对你有帮助!

相关问题