感谢@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();
}
}
}