私はcodeigniterを初めて使用しています。登録後にログインしようとしています...正しいユーザー名とパスワードを入力しましたが、そのようなアカウントは存在しません "というメッセージをelse
に設定します。登録後にログインできません(エコーnum_rows()は0ですがレコードがあります)
私は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