问题描述
如果用户符合我存储在数据库中的到期时间,我正尝试从网站注销该用户。
我创建了一个名为 user_sessions 的表,其中存储了当前的 userid , logintime(varchar), expirytime(varchar) ),并且我在到期时间字段中的登录时间上增加了1分钟,因此当用户登录时,所有这些值都将存储在db中。
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.
我存储在像这样的db
I have stored in db like this
这是我的模型:
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 文件 \application\models\Common_model.php
在 autoload.php 中添加common_model \application\config\autoload.php
Add common_model in 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');
}
}
?>
希望这对您有帮助!
这篇关于如果用户在Codeigniter中从数据库中注销了到期时间,则从网站注销用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!