我是codeigniter的新手,我試圖登錄後登錄...我輸入正確的用戶名和密碼,但錯誤是「在數據庫中不存在這樣的帳戶「,如果該消息爲假,則在else
部分中設置消息。我註冊後無法登錄(雖然檢查:回聲num_rows()爲0,雖然有記錄)
檢查我用echo num_rows()
它顯示0
(這是錯誤的),共有5條記錄在那裏,但echo num_fields
顯示8個字段(這是正確的)。
控制器登錄代碼:
public function login() {
$this->form_validation->set_rules('username', 'User Name', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('login');
} else {
//get user name
$username = $this->input->post('username');
//get encrypt password
$password = md5($this->input->post('password'));
//login user
$user_id = $this->auth_model->login($username, $password);
if ($user_id) {
//create session
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata($user_data);
//set message
$this->session->set_flashdata("success", "your are logged in");
redirect('index.php/users/profile');
} else {
$this->session->set_flashdata("error", "No such account exists in database");
redirect('index.php/auth/login');
}
}
}
模型登錄代碼:
public function login($username, $password) {
$this->db->where('username', $username);
$this->db->where('password', $password);
$result = $this->db->get('tblLogin');
if ($result->num_rows() == 1) {
return $result->row(0)->id;
} else {
return false;
}
}
不要在密碼上使用md5! – Twinfriends
thanq,是的,我知道了..但如何加密密碼?不需要它加密密碼 –
試試這個[教程](https://www.sitepoint.com/hashing-passwords-php-5-5-password-hashing-api/)。你是否也試過用你的手來檢查sql是否返回任何內容? – Vastlik