2017-08-10 17 views
0

私はCodeIgniterフレームワークを使用しています。 以下は、Signup.phpコントローラに含まれる機能です。ハッシュされたパスワードを確認する方法は?

public function _hash_string($str){ 
      $hashed_string = password_hash($str, PASSWORD_BCRYPT); 
      return $hashed_string; 
     } 

     public function _verify_hash($text, $hashed_string){ 
      $result = password_verify($text, $hashed_string); 
      return result; //TRUE OR FALSE 
     } 

     public function index() 
     { 
      if($this->input->post('newuser') == 1) 
      { 
       $user = new Users_model(); 
       $user->username = $this->input->post('username'); 
       $user->email = $this->input->post('email'); 
       $pass= $this->input->post('password'); 
       $hashed_pass = $this ->_hash_string($pass); 
       $user->password = $hashed_pass; 
       $user->account_status = 1; 
       $user->user_role = $this->input->post('user_role'); 
       $id = $this->usermodel->insert($user); 
}else{ 
      $this->load->view('signup-page'); 
     } 

私は正常にユーザーのパスワードをハッシュしました。どうすればそれらを確認できますか? 以下は、Login.phpコントローラに含まれる機能です。

public function index() 
    { 
     if($this->input->post('login') == 1) 
     { 
      $user = new Users_model(); 
      $user->email = $this->input->post('email'); 
      $user->password = $this->input->post('password'); 
      $user->user_role = $this->input->post('user_role'); 
      $results = $this->usermodel->login($user); 
      if(count($results) > 0) 
      { 
       foreach($results as $row) 
       { 
        $session_array = array(
         "id" => $row['id'], 
         "username" => $row['username'], 
         "email" => $row['email'], 
         "password" => $row['password'], 
         "account_status" => $row['account_status'], 
         "user_role" => $row['user_role'] 
        ); 
        $this->session->set_userdata($session_array); 
        $url = base_url() . "home?login=success"; 
        redirect($url, "refresh"); 
       } 
      }else{ 
       $url = base_url() . "login?login=failed"; 
       redirect($url, "refresh"); 
      } 
     }else{ 
      $this->load->view('login-page'); 
     } 
    } 

ここにUsers_model.phpモデルがあります。

function login($user){ 
     $conditions = array(
        "email" => $user->email, 
        "password" => $user->password, 
        "user_role" => $user->user_role, 
        "account_status" => 1, 
       ); 
     $this->db->select('*'); 
     $this->db->from('users'); 
     $this->db->where($conditions); 
     $rs= $this->db->get(); 
     return $rs->result_array(); 
    } 

ユーザーを正しくログインさせるにはどうすればよいですか?

+2

あなたは既に '_verify_hash'という関数を書いています。どうしたの? – Quentin

+0

申し訳ありませんが、login.phpコントローラまたはUsers_model.phpモデルの中で、どこで使用すればよいか分かりませんでしたか? –

+0

@QiYangそれは設計上の質問です。あなたまで – Ice76

答えて

-1

mysqlのパスワードを比較することはできません。ので、PHPを使用してハッシュ、あなたはパスワードを比較するPHP関数を使用する必要があります。

function login($user){ 
    $conditions = array(
       "email" => $user->email, 
       //"password" => $user->password, 
       "user_role" => $user->user_role, 
       "account_status" => 1, 
      ); 
    $this->db->select('*'); 
    $this->db->from('users'); 
    $this->db->where($conditions); 
    $rs= $this->db->get(); 
    if(!empty($rs)) { 
     $result_array = $rs->row_array(); 
     $controllerInstance = & get_instance(); 
if($controllerInstance->_verify_hash($user->password,$result_array['password']) == TRUE) { 
     return $result_array; 
     } 
    } 
    return false; 
} 

コントローラにアクセスするにはget instanceを使用します。 $ controllerInstance = & get_instance(); $ controllerData = $ controllerInstance - > _ verify_hash();

これが役に立ちます。 zaphのコメントの後に更新されました

+0

暗号化されておらず、ハッシュされています。 – zaph

+0

@zaphパスワードを確認する方法は? –

+0

['password_hash'](http://php.net/manual/en/function.password-hash.php)を使用する場合、[' password_verify']で確認してください(http://php.net/manual/en/function .password-verify.php)。リンクの例を参照してください。 – zaph

関連する問題