2017-08-24 36 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