0

我正在创建一个登录表单,但它没有正确地对数据库中的值进行验证。当我输入错误的密码时,它仍然会将我重定向到需要登录访问的页面

控制器

public function login() {

    $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]|max_length[12]|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[6]|max_length[6]|xss_clean');

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

        // return main page if submitted form is invalid.

        $this->load->view('abt_login');

    } else {

         $this->load->model('abt_db');
    $q = $this->abt_db->check_login();


    if($q)
    {
        $data = array(
            'email'         => $this->input->post('email'),
            'password'      => $this->input->post('password'),
        );

        $this->session->set_userdata($data);
        redirect('index.php/abovetheblues/abt-abovetheblues');
    }
    else
    {
       redirect('index.php/abovetheblues/abt_selfhelp');
    }


    }
}

模型

function check_login() {
        $this->load->database();

        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', $this->input->post('password'));
        $q = $this->db->get('user');

        if ($q->num_rows == 1) {
            return true;
        }
    }
4

2 回答 2

1

num_rows是一个函数,你不调用函数
尝试改变这个:

if ($q->num_rows == 1) {
    return true;
}

对此:

if ($q->num_rows() == 1) {
    return true;
}
于 2013-08-06T16:11:23.973 回答
0

考虑将这两个变量传递给函数,而不是尝试从输入类中获取它们:

控制器:

$this->load->model('abt_db');
$q = $this->abt_db->check_login($this->input->post('email'), $this->input->post('password'));

模型:

function check_login($email, $password) {
    $this->load->database();

    $this->db->where('email', $email);
    $this->db->where('password', $password);  ## VERY UNSECURE - SEE BELOW
    $q = $this->db->get('user');

    if ($q->num_rows() == 1) { ## as seen in the previous answer
        return true;
    }
    return false; ## You NEED to have this. 
}

请注意:

您正在以明文形式直接使用数据库检查密码。这是一个糟糕的想法,会导致您的应用程序受到损害。

我强烈建议您阅读这本关于密码安全的优秀入门书:

盐渍密码散列

于 2013-08-06T16:24:03.787 回答