我尝试使用此简单代码登录,但是在单击登录按钮后,其他选择为“无效的用户名或密码!”。即使用户名和密码匹配也总是出现
控制器:
class Auth100 extends CI_Controller{ public function __construct(){ parent::__construct(); $this->load->model('AuthModel100'); } public function login(){ if($this->input->post('login') && $this->validation('login')){ $login=$this->AuthModel100->getuser($this->input->post('username')); if($login !=NULL){ if(password_verify($this->input->post('password'), $login->password_100)){ } } $this->session->set_flashdata('msg','Invalid Username or Password!'); } $this->load->view('auth100/form_login_100'); } public function logout(){ redirect('auth100/login'); } public function changepass(){ if($this->input->post('change') && $this->validation('change')){ redirect('welcome'); } $this->load->view('auth100/form_password_100'); } public function validation($type){ $this->load->library('form_validation'); if($type=='login'){ $this->form_validation->set_rules('username', 'Username', 'required'); $this->form_validation->set_rules('password', 'Password', 'required'); } else { $this->form_validation->set_rules('oldpassword', 'Old Password', 'required'); $this->form_validation->set_rules('newpassword', 'New Password', 'required'); } if($this->form_validation->run()) return TRUE; else return FALSE; }
型号:
class AuthModel100 extends CI_Model{ public function getuser($username){ $this->db->where('username_100', $username); return $this->db->get('users100')->row(); } }
查看:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Catshop100 | Login</title>
</head>
<body>
<div class="container" style="width:50%;">
<h3>Kitty Fan Shop - Login</h3>
<hr>
<div style="color: red;"><?=validation_errors()?></div>
<form action="" class="form-horizontal" method="post">
<div class="form-group">
<label for="username" class="control-label col-sm-2">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="username">
</div>
</div>
<div class="form-group">
<label for="password" class="control-label col-sm-2">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" name="login" value="Login" class="btn btn-default">
</div>
</div>
</form>
</div>
</body>
</html>
请帮助我解决此问题
答案 0 :(得分:0)
由于错误代码不在else条件下,因此您收到错误消息,我在下面编写了经过纠正的代码,并附有必要的注释。看看是否对您有帮助。
public function login(){
if($this->input->post('login') && $this->validation('login')){
$login=$this->AuthModel100->getuser($this->input->post('username'));
if($login !=NULL){
if(password_verify($this->input->post('password'), $login->password_100)){ // success
// do something -- redirect to dashboard -- your logic
}else{ // fail
$this->session->set_flashdata('msg','Invalid Username or Password!');
$this->load->view('auth100/form_login_100');
}
}else{
// do something if $login is NULL
}
}
}
答案 1 :(得分:0)
尝试一次,您需要处理else条件,以便该方法可以将发生的事情返回给您。
public function login() {
if ($this->input->post('login') && $this->validation('login')) {
$login = $this->AuthModel100->getuser($this->input->post('username'));
if ($login != NULL) {
if (password_verify($this->input->post('password'), $login->password_100)) {
$this->session->set_flashdata('msg', 'Login Successful!');
} else {
$this->session->set_flashdata('msg', 'You have entered an invalid password');
}
} else {
$this->session->set_flashdata('msg', 'You have not entered username');
}
}
$this->load->view('auth100/form_login_100');
}