2017-08-24 35 views
1

我正在開發iOS應用程序的登錄系統。在PHP中使用password_verify

這是我使用的發送JSON響應到應用程序的PHP腳本的一部分:

if(isset($_POST['correo']) && isset($_POST['pass']) && $_POST['key'] == "123456") 
{ 


    $password = $_POST['pass']; 



    $q = mysqli_query($mysqli,"SELECT * FROM users WHERE email = '".$_POST['correo']."' AND 
    encrypted_password = '".$_POST['pass']."'") or die (mysqli_error()); 

    if(mysqli_num_rows($q) >= 1){ 
     $r = mysqli_fetch_array($q); 

// this is the hash of the password in above example 
      $hash = $r['encrypted_password']; 

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

       $results = Array("error" => "1","mensaje" => "su ID es ".$r['id'],"nombre" => $r['nombre'],"apellidos" => $r['apellidos'],"email" => $r['email'], 
     "imagen" => $r['imagen'],"unidad" => $r['unidad']); 

      } else { 
       $results = Array("error" => "2","mensaje" => " acceso denegado "); 
      } 

    }else{ 
     $results = Array("error" => "3","mensaje" => "no existe"); 
    } 

} 

echo json_encode($results); 

我的問題是關於在PHP中使用password_verify。 我想知道它是否應該像在腳本中一樣工作,那麼應用程序中不會收到JSON響應。

謝謝

+4

您mysqli_query總會回報空結果,因爲你傳遞查詢未加密的值並試圖使其等於加密'encrypted_pa​​ssword ='「。$ _ POST ['pass']' – diavolic

+1

請參數化您的查詢。 – chris85

+0

您最初是如何存儲用戶密碼的? 'encrypted'!='hahsed'(它應該被散列)。 – chris85

回答

1

你並不需要在WHERE條件匹配password,是這樣的:

$q = mysqli_query($mysqli,"SELECT * FROM users WHERE email = '".$_POST['correo']."'") or die (mysqli_error()); 

     if(mysqli_num_rows($q) >= 1){ 
       $r = mysqli_fetch_array($q); 



       // this is the hash of the password in above example 
       $hash = $r['encrypted_password']; 

       //you need to match the password from form post against password from DB using password_hash  

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

防止SQL注入使用參數化查詢編號:How can I prevent SQL injection in PHP?

+0

...和SQL注入在這裏,'WHERE email ='「。$ _ POST ['correo']。」'「'' – chris85