用户登录状态激活检查codeigniter?

时间:2017-05-19 01:47:14

标签: php codeigniter codeigniter-2

我是Codeigniter的新人。我有一个登录系统,状态为0的用户尚无法登录,状态为1的用户可以登录。我的代码可能有错误。所以,我希望你能找到我的错误在哪里,并帮助我做到正确。这是我的代码。

我的控制器

public function login() {
            $this->form_validation->set_rules('no', 'No', 'required|min_length[10]|max_length[16]|integer');
            $this->form_validation->set_rules('password', 'password', 'required|md5|xss_clean');
            $this->form_validation->set_error_delimiters('<span class="error">', '</span>');

            if($this->form_validation->run()== FALSE) {
                $this->load->view('v_login');
            }else{
                    $no = $this->input->post('no');
                    $password = $this->input->post('password');
                    $cek = $this->m_user->ambilPengguna($no, $password);
                    $status = $this->m_user->ambilStatus($no); //HERE'S THE PROBLEM
                    if($cek->num_rows()<> 0 && $status == '1') { //HERE'S TOO, IT WON'T CHECK THE STATUS.
                            $this->session->set_userdata('isLogin', TRUE);
                            $this->session->set_userdata('data_user',$cek->row());
                            redirect('c_belajar');
                    }else {
                        echo " <script>
                                    alert('Login failed! call the administrator to activate your account');
                                    history.go(-1);
                        </script>";        
                }
            }  
        }

我的模特

public function ambilPengguna($no, $password) {
                $this->db->select('*');
                $this->db->from('tb_user');
                $this->db->where('no_id', $no);
                $this->db->where('password', $password);


                $query = $this->db->get();

            return $query;
        }

        public function ambilStatus($no){
            $this->db->select('status');
            $this->db->from('tb_user');
            $this->db->where('no_id', $no);
            $query = $this->db->get();

            return $query;
        }

控制器出现了错误。请帮帮我。

2 个答案:

答案 0 :(得分:0)

好的!我可以建议一些代码重组

<强>控制器

class YourController extends CI_Controller {
function __construct()
{
    parent::__construct();
    $this->load->model('login_model');
    $this->load->library('form_validation');
}

public function login()
{
    if($_POST)
    {
        $config=array(
            array(
                'field' => 'no',
                'label' => 'Number',
                'rules' => 'trim|required',
            ),
            array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'trim|required',
            )
        );
        $this->form_validation->set_rules($config);
        if($this->form_validation->run()==false)
        {
            $data['errors']=validation_errors();
            $this->load->view('login',$data);
        }
        else
        {
            $check=$this->login_model->checkUser($_POST); // you can use xss clean here filter post data
            if(!$check)
            {
                $data['errors']='Invalid Password';
                $this->load->view('login',$data);
            }
            elseif($check==1)
            {
                $data['errors']='Your account status is not active yet, Please contact Administrator';
                $this->load->view('login',$data);
            }
            else
            {
                $this->session->set_uerdata($check);
                redirect(base_url().'dashboard');
            }
        }
    }
    else
    {
        $this->load->view('login');
    }
 }
}

<强>模型

class login_model extends CI_Model {
function __construct()
{
    parent::__construct();
}

public function checkUser($data)
{
    $st=$this->db->select('*')
        ->from('tbl_user')
        ->Where('no_id', $data['no'])
        ->where('password', $data['password'])
        ->get()->result_array();// you can use row()
    if(count($st)>0)
    {
        if($st[0]['status']==0){
            return 1;
        }
        else
        {
            return $st[0];
        }
    }
    else
    {
        return false;
    }
}
}

答案 1 :(得分:0)

试试这个,这在我的项目中工作..

你的模特

    public function ambilStatus(){

        $this->db->where('no_id', $this->input->post('your input name'));
        $query = $this->db->get($this->db->dbprefix . 'tb_user');
        $ret = $query->row();
        return $ret->account_status;

    }

和您的控制器

        $status = $this->m_user->ambilStatus();
        if($status && $cek->num_rows() == 1 ) {