Codeigniter form_validation不验证

时间:2016-02-04 09:23:51

标签: php forms codeigniter validation login

我目前正在处理我的登录表单。昨天当我登录时,即使凭据有误,它也会让我登录,因此我在模型中将=更改为此代码的==

public function can_log_in() {

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

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

现在即使凭据是正确的,它也不会登录。这是我的控制器:

public function loginValidate() {

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

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

    if ($this->form_validation->run() == FALSE) {
        $this->index();
    } else {
        $data = array(
            'USERNAME' => $this->input->post('USERNAME'),
            'is_logged_in' => 1
        );
        $this->session->set_userdata($data);

        redirect('login/adminIndex'); 
    }
}

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

1 个答案:

答案 0 :(得分:0)

if($query->num_rows==1)

附近的问题模型函数中

您更改为if($query->num_rows()==1)

模型功能中的完整代码

public function can_log_in(){

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

$this->db
$query = $this->db->query($sql, $data1);


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

    return true;

}else{

    return false;
}