检查用户是否不活跃,不登录 [英] Check if user is not active, not to log in

查看:60
本文介绍了检查用户是否不活跃,不登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的登录表单添加条件-如果用户尚未激活,则不要登录.我正在使用CodeIgniter.那是我的控制器:

I want to add condition for my login form - if a user is not already active, not to log in. I'm using CodeIgniter. That's my controller:

public function login ()
    {
            
        $this->load->model('user_model');
        $user=$this->user_model->login();
        
        $this->form_validation->set_rules('username', 'Username', 'trim|required|callback_login_check');
        $this->form_validation->set_rules('password', 'Password', 'trim|required'); 

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

            $this->index();
        }
        else 
        {
            if(count($user) > 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);         
                
                redirect('index/home_page');
            }
        }
    }

我的模特是:

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

我在登录功能中尝试过此操作:$this->db->where('deactivated_at = "0000-00-00 00:00:00" OR deactivated_at IS NULL');,但是它不起作用.如果不是活动用户,该如何进行身份验证,根本不登录?

I have tried with this: $this->db->where('deactivated_at = "0000-00-00 00:00:00" OR deactivated_at IS NULL'); in my login function, but it does not work. How could I make this authentication, if not active user, not to log in at all?

推荐答案

我的代码中有几句话是错误的.

There's a few things I would say is wrong with your code.

首先,您要在表单验证完成之前尝试登录用户.应该在之后执行此操作,否则我看不到需要验证吗?

First off, you're trying to login the user before the form validation has completed. This should be done afterwards, or I don't see the need for validation?

这是我在控制器中的登录功能版本.

This would be my version of your login function, within your controller.

function login()
{
  $this->load->model('users_model');

  $this->form_validation->set_rules('username', 'Username', 'trim|required');
  $this->form_validation->set_rules('password', 'Password', 'trim|required');

  if (!$this->form_validation->run())
  {
    $this->index(); // Show the login form..
  }
  else
  {
    // This is where we try to login the user, now the validation has    passed
    if ($user = $this->users_model->login())
    {
      // Start the session...
    }
    else
    {
      // The model returned false..
    }
  }
}

因此,在通过表单验证之前,您无需进入模型.然后,在您的模型中;

So you don't go to the model, until the form validation has passed. Then, in your model;

function login()
{
  $where = array(
    'username' => $this->input->post('username'),
    'password' => sha1($this->input->post('password'))
  );
  // $this->db->select('*'); No need for this line
  $query = $this->db->get_where('users', $where);
  if ($query->num_rows() > 0)
  {
    // Found a match
    // Are they activated?
    if (!is_null($query->row('deactivated_at'))
    {
      // The user isn't deactivated
      return $query->row_array();
    }
    else
    {
      // The user is deactivated
      return false;
    }
  }
  else
  {
    // The username and/or password is wrong...
    return false;
  }
}

希望这会有所帮助.

这篇关于检查用户是否不活跃,不登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆