2016-09-07 2 views
0

이 코드는 Codeigniter (버전 3.0) 로그인 모듈에 있습니다. 그것은 잘 작동하지만이 안전합니까? PHP password_verify를 사용하여 로그인 및 암호를 확인하는 더 좋은 해결책이 있습니까? (PHP 5.6, MySQL 5.0).Codeigniter password_verify 메서드

 $user = $this->input->post('username'); 
     $password = $this->input->post('password'); 
     $myquery = $this->db->query("SELECT * FROM users WHERE user = '$user'"); 
     $row = $myquery->row(); 

     if (isset($row)) 
     { 
      //Using hashed password - PASSWORD_BCRYPT method - from database 
      $hash = $row->password; 


      if (password_verify($password, $hash)) { 

       echo 'Password is valid!'; 


      } else { 

       echo 'Invalid password.'; 

      } 


     } else{ 

      echo 'Wrong user!'; 
     } 
+1

저는 **이 ** PHP 함수'password_verify()'입니다.이 함수는 안전하고 사용하는 것이 좋습니다. 더 중요한 것은 해시가 생성되는 방법이지만,'password_hash()'함수가 호출 된 것 같습니다. – martinstoeckli

답변

3

코드는 잘 보이지만 당신은 SQL 주입에 의해 보호되고,이 경우에는 좀 더 클리너 CI 방법에 조금 더 그것을 할 수 있습니다 당신은 좀 더 캡슐화에게

시도가 이 같은 :

public function checkLogin() 
{ 
    $this->load->library("form_validation"); 

    $arrLoginRules = array 
    (
     array(
      "field" => "username", 
      "label" => "Benutzername", 
      "rules" => "trim|required" 
     ), 
     array(
      "field" => "password", 
      "label" => "Passwort", 
      "rules" => "trim|required" 
     ) 

    ); 

    $this->form_validation->set_rules($arrLoginRules); 

    try 
    { 
     if (!$this->form_validation->run()) throw new UnexpectedValueException(validation_errors()); 

     $user = $this->input->post('username'); 
     $password = $this->input->post('password'); 
     $query = $this->db 
      ->select("*") 
      ->from("users") 
      ->where("user", $user) 
      ->get(); 

     if ($query->num_rows() != 1) throw new UnexpectedValueException("Wrong user!"); 

     $row = $query->row(); 
     if (!password_verify($password, $row->hash)) throw new UnexpectedValueException("Invalid password!"); 

     echo "valid user"; 

    } 
    catch(Excecption $e) 
    { 
     echo $e->getMessage(); 
    } 
} 

위험 해요 자세한 내용은 Form validation 에서 모양과 Query Builder 문서를 가져 가라.

+0

조언 해 주셔서 감사합니다. –

+0

답변이 도움이된다면 pls를 수락하십시오.) – sintakonte

관련 문제