在代码点火器

时间:2016-02-05 12:40:27

标签: php sql codeigniter admin

这是我的代码,我试图通过数据库进行管理员访问...但似乎我的usertype验证没有得到任何东西。我试图print_r它,输出只是数组(),你认为我的错误是什么。这是我的代码..

登录控制器

public function loginValidate(){

    $this->load->library('form_validation');
    $this->load->model('login_model'); 

    $this->form_validation->set_rules('USERNAME','Username','required|trim|callback_validateCreds');
    $this->form_validation->set_rules('PASSWORD','Password','required|trim|md5');

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

        $this->index();

    }else{

        $data = array(

            'USERNAME' => $this->input->post('USERNAME'),
            'is_logged_in' => 1,
            'value' => $this->login_model->returnQuery()

            );

        $this->session->set_userdata($data);

        redirect('login/adminIndex');

        $q = $this->login_model->returnQuery();

            if ($q = '1'){

                redirect('login/adminIndex');

            }elseif ($q = '2') {

                redirect('login/userIndex');

            }else{

                redirect('login/restricted');

        }

    }
} 

public function validateCreds(){

        $this->load->library('form_validation');
        $this->load->model('login_model');

        if($this->login_model->can_log_in()){ // can_log_in() model

            return true;

        }else{

            $this->form_validation->set_message('validateCreds','Username and password did not match.');  //return message to validateCreds

            return false;
        }

    }

这是我的模特

public function can_log_in(){

    $sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?";
    $data = array(
        'USERNAME' => $this->input->post('USERNAME'),
        'PASSWORD' => $this->input->post('PASSWORD')
        );
    $query = $this->db->query($sql, $data);

    return $query;


    if ($query->num_rows() == 1){

        return true;

    }else{

        return false;
    }

}

public function returnQuery(){


    $sql = "SELECT USERCODE FROM tbl_users where USERNAME =? AND PASSWORD = ?";
    $data = array(
        'USERNAME' => $this->input->post('USERNAME'),
        'PASSWORD' => $this->input->post('PASSWORD')
        );
    $query =  $this->db->query($sql, $data);


    return $query->result();
}

没有任何错误,但返回true

            if ($q = '1'){

                redirect('login/adminIndex');

即使它不是= 1 ..我也试过==然后起飞''但它只会指引我到限制页面。

1 个答案:

答案 0 :(得分:0)

首先请记住,您返回的是完整数组而不是单个值,因此将模型函数更改为:

public function returnQuery(){ 
   $sql = "SELECT USERCODE FROM tbl_users where USERNAME =? AND PASSWORD = ?"; 
   $data = array( 'USERNAME' => $this->input->post('USERNAME'), 'PASSWORD' => $this->input->post('PASSWORD') ); 
   $query = $this->db->query($sql, $data); 
   $result = $query->result_array(); 

   if(count($result) > 0){
      return $result[0]["USERCODE"];
   }
   else{
      return 0;
   }
}

比比较值不指定值:

if($q == 1){
   //your redirection stuff
}
elseif($q == 2){
    //your redirection stuff
}
else{
    //your redirection stuff
}

更新1:

您还在回调函数中发出问题。在检查记录数之前,您正在使用return $query

修改功能

public function can_log_in(){ 
   $sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?"; 
   $data = array( 'USERNAME' => $this->input->post('USERNAME'), 'PASSWORD' => $this->input->post('PASSWORD') ); 
   $query = $this->db->query($sql, $data); 
   //return $query;

   if ($query->num_rows() == 1)
   { 
      return true; 
   }
   else{ 
      return false; 
   } 
}