-2

感谢@Mischa 在这里帮助我。

回答 :

模型 :

function validate_login($username, $password) {
        $bcrypt = new Bcrypt(17);

        $sql = "SELECT * FROM users WHERE username = ? ";
        $loginQ = $this -> db -> query ($sql, array($username));
        $database = $loginQ->row();

        $hash = $database->password;
        if ($bcrypt -> verify($password, $hash)){
            return $loginQ;
        }
    }

控制器 :

function validate_credentials() {
        $this -> load -> library('form_validation');
        $this -> load -> library('bcrypt'); 

        $this -> form_validation -> set_rules('username', 'Username', 'required|alpha_numeric|min_length[4]|max_length[15]');
        $this -> form_validation -> set_rules('password', 'Password', 'required|min_length[7]|alpha_dash|max_length[20]');

        if ($this -> form_validation -> run() == FALSE) {
            $this -> index();
        } else {
            $this -> load -> library('bcrypt');     
            $this -> load -> model('login_model');

            $username = $this -> input -> post('username');
            $password= $this -> input -> post('password');

            if ($loginQ = $this -> login_model -> validate_login($username, $password)) {
                if ($activated = $this -> login_model -> activated($username)) {
                    $session_array = array('username' => $this -> input -> post('username'), 'loggedin' => TRUE);
                    $this -> session -> set_userdata($session_array);
                    redirect('staff_controller/index');
                } else {
                    $this -> session -> sess_destroy();
                    $this -> load -> view('accessdenied_view');
                    $this -> output -> _display();
                    die();
                }
            } else {
                $this -> index();
            }
        }
    }
4

1 回答 1

1

您尝试执行的操作仅在将数据传递给视图时才有效。您只需将单独的变量传递给您的模型。像这样的东西:

function validate_login($username, $password) {
  $bcrypt = new Bcrypt(17);

  $sql = "SELECT * FROM users WHERE username = ? LIMIT ? ";
  $loginQ = $this -> db -> query ($sql, array($username, 1));
  $row = $loginQ->result();

  $hash = $row['password'];
  if ($brcrypt -> verify($password, $hash)){
    return $loginQ;
  }
}

当然,这意味着您还必须更改控制器代码以分别传递这些变量。

另一种选择是在您的模型中使用$date['username']$data['password'],但我不建议这样做,因为它会使您的代码更难阅读。

更新以使其更清晰:

控制器:

$username = $this->input->post('username');
$password = $this->input->post('password');
$this->login_model->validate_login($username, $password);

模型:

function validate_login($username, $password) {
  // Etc.
}
于 2013-02-21T14:08:01.533 回答