如果用户符合我存储在数据库中的到期时间,我将尝试从网站注销。我创建了一个名为user_sessions的表,其中存储了当前username、logintime(varchar(和expirytime(varchar。
所以在用户登录后,他们会重定向到仪表板,所以如果用户在一分钟内没有在网站上做任何事情,如果他们试图访问下一个页面,他们应该从网站注销。
如果用户继续移动到下一个页面,每次他们移动到下个页面时,我都需要在登录时间字段中增加一分钟的额外时间。
我已经像这个一样存储在数据库中
这是我的型号:
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);
}
}
}
有人能帮我怎么做吗。
提前谢谢。
请按照以下步骤操作:
创建Common_model.php文件applicationmodelsCommon_model.php
在autoload.php中添加common_modelapplicationconfigautoload.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');
}
}
?>
希望这对你有帮助!