登录表单未经验证

时间:2013-08-06 16:09:06

标签: php codeigniter

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

CONTROLLER

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


    }
}

MODEL

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

2 个答案:

答案 0 :(得分:1)

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

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

到此:

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

答案 1 :(得分: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. 
}

请注意:

您正在使用纯文本数据库直接检查密码。这是一个 糟糕的 主意,会导致您的应用程序遭到入侵。

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

Salting Password Hashing

相关问题