感谢@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();
}
}
}
答案 0 :(得分: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.
}