如何检查用户是否处于活动状态,然后注销

时间:2015-04-18 11:37:17

标签: codeigniter login

我有一个登录表单,它可以工作,但如果用户是否有效,我必须添加条件。如果他不活跃,则重定向到某个页面show_error。我正在使用CodeIgniter。这是我的模型:我尝试使用此功能“is_active_user”,但它无效。

 public function login()
    {
        $this->db->select('*'); 
        $this->db->from('users');     
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password',sha1($this->input->post('password')));
        $result=$this->db->get();
            return $result->row_array();            
    }
  public function is_active_user() {
        $this->db->select('*');
        $this->db->from('users');         
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('deactivated_at = "0000-00-00 00:00:00" || deactivated_at IS NULL ');
        $result=$this->db->get();
          
            if($result->num_rows() > 0)
            {
                return $result->row_array();
                
            } 
            return false;
             
    }

我的控制器是:

 public function login ()
    {
            
        $this->load->model('user_model');
        $user=$this->user_model->login();
        
        $is_active=$this->user_model->is_active_user();
 $this->form_validation->set_rules('username', 'Потребителско име', 'trim|required|callback_login_check');
        $this->form_validation->set_rules('password', 'Парола', 'trim|required'); 

        if ($this->form_validation->run()==FALSE)
        {

            $this->index();
        }
        else 
        {
            if(count($user) > 0 && count($is_active) > 0)    
            {
                $this->load->library('session'); 
            
                 $data = array(
                    'username' => $user['username'],
                    'user_id' => $user['user_id'],
                    'is_logged_in' => TRUE,
                    'role_id' => $user['role_id']
                
                );
                $this->session->set_userdata($data);         
           
            }
        }
    }

如何检查用户是否处于活动状态?

但没有结果。 现在我尝试了:

 public function login()
    {

        $this->db->select('*'); 
        $this->db->from('users');     
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password',sha1($this->input->post('password')));
        $this->db->where('deactivated_at = "0000-00-00 00:00:00" OR deactivated_at IS NULL');
        
        $result=$this->db->get();
            return $result->row_array();            
    }

但它记录了另一个配置文件,而不是我填写的用户名和密码的配置文件。

1 个答案:

答案 0 :(得分:1)

public function login()
{
    $this->db->select('*'); 
    $this->db->from('users');     
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password',sha1($this->input->post('password')));
    $this->db->where('deactivated_at <> "0000-00-00 00:00:00"');
    $this->db->where('deactivated_at IS NOT NULL');
    $result=$this->db->get();
    return $result->row_array();            
}