登录使用错误的用户名和密码访问帐户

时间:2013-12-05 06:18:53

标签: php codeigniter

在下面的代码中,我放置了控制器和modl。当用户处于非活动状态时,他不应该访问该帐户,只有创建的帐户用户才能访问该帐户。但在我的情况下,它不能像那样工作.Pls帮我纠正这个问题。 控制器:

function validate_credentials()
        {   $this->load->helper('url');

            $this->load->model('membership_model');
            $query = $this->membership_model->validate();

            if($query) // if the user's credentials validated...
            {
                $data = array(
                    'username' => $this->input->post('username'),
                    'is_logged_in' => true
                );
                 $data1 = array(
                    'college_name' => $this->input->post('college_name'),
                    'is_logged_in' => true
                );
                 $this->session->set_userdata($data1);
                 redirect('site1/members_area');
                if($query->num_rows()>0){
                 $status = $query->row()->account_status;}
                else {
                 $status = ''; }
                 //Account active
                if($status == 'active')
                {
                   $this->session->set_userdata($data);
                   redirect('site1/members_area');
                }
                else  if ($status == 'inactive')//Account In active
                {  $this->inactive();
                  }
                  else // incorrect username or password
            {
                $this->invalid();
            }
            }

        }   

    function inactive()
        {
        echo"<script>alert('In active user Please contact the administrator');</script>";
        $this->load->view('login_form'); 
        }
        function invalid()
        {
        echo"<script>alert('Invalid username or password');</script>";
        $this->load->view('login_form'); 
        }

站点1

function members_area()
    {
        $this->load->view('index');


    }

模型:

function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
            $this->db->where('college_name', $this->input->post('college_name'));
        $query = $this->db->get('membership');

        return $query;

    }

2 个答案:

答案 0 :(得分:0)

在你的模特中

return $query->num_rows();  

在你的控制器中用

替换if($ query)
if($query>0){ ..... your code ....}

答案 1 :(得分:0)

function validate_credentials()
    {   $this->load->helper('url');

        $this->load->model('membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
             $data1 = array(
                'college_name' => $this->input->post('college_name'),
                'is_logged_in' => true
            );
             $this->session->set_userdata($data1);

            if($query->num_rows()>0){
             $status = $query->row()->account_status;}
            else {
             $status = ''; }
             //Account active
            if($status == 'active')
            {
               $this->session->set_userdata($data);
               redirect('site1/members_area');
            }
            else  if ($status == 'inactive')//Account In active
            {  $this->inactive();
              }
              else // incorrect username or password
        {
            $this->invalid();
        }
        }

    }