【问题标题】:Logout the user from website if they meet the expiry time from database in codeigniter如果用户在 codeigniter 中遇到数据库的到期时间,则从网站注销用户
【发布时间】:2018-07-11 09:39:02
【问题描述】:

如果用户达到我存储在数据库中的到期时间,我会尝试从网站上注销用户。 我创建了一个名为 user_sessions 的表,其中存储了当前 useridlogintime(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并定义成autoload.php。这样你只需要在单个文件中编写代码。希望对您有所帮助!
  • 不清楚。能否请您简单解释一下。如果您有空,请您一步一步告诉我,以便我编写代码

标签: mysql codeigniter


【解决方案1】:

请按照以下步骤操作:

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

autoload.php中添加common_model \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');
    }
}
?>

希望对您有所帮助!

【讨论】:

  • 给我 5 分钟让我检查一下
  • redirect('home', 'refresh'); //这将重定向到登录页面或仪表板页面
  • 以上代码重定向到主页,但您可以将其更改为默认控制器。即登录。 redirect('controller_name', 'refresh');
  • 当我喜欢这个重定向('Login');它显示此错误此页面无法正常工作
  • 登录是我的控制器名称..它只重定向到正确的 URL,但查看页面不可见
猜你喜欢
  • 2010-12-27
  • 2015-01-16
  • 1970-01-01
  • 2017-08-17
  • 2012-04-19
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多