2017-09-27 5 views
0

私は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; 
    } 
} 
+0

パスワードにmd5を使用しないでください! – Twinfriends

+0

thanq、はい私はそれを得た..しかし、パスワードを暗号化する方法?それを必要としないパスワード –

+0

この[チュートリアル](https://www.sitepoint.com/hashing-passwords-php-5-5-password-hashing-api/)を試してみてください。また、何かを返す場合は、あなたの手でチェックをそのSQLを試してみましたか? – Vastlik

答えて

0
public function login($username, $password) { 

    $this->db->where('username', $username); 
    $this->db->where('password', $password); 
    $result = $this->db->get('tblLogin'); 
    $ret = $result->row(); 

    if ($ret) { 
     return $ret; 
    } else { 
     return false; 
    } 
} 

行()関数の取得が単一であり、条件場合にチェック。 $this->db->last_query(); を使用してクエリを出力し、mysqlデータベースを確認してください。

0

あなたの問題のため、この解決策を試すことができます。

はあなたのモーダル機能を変更します:

Modal.php

public function login($username, $password) { 
    $this->db->where('username', $username); 
    $this->db->where('md5(password)', $password); 
    $result = $this->db->get('tblLogin'); 
    $ret = $result->row(); 
    if (!empty($ret)) { 
     return $ret; 
    } else { 
     return false; 
    } 
} 

をお使いのコントローラファイルを変更してください。 LoginController.php

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 = $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'); 
     } 
    } 
} 
+0

同じエラーが発生していません... md5を実行中に削除します –

関連する問題