mvasco
mvasco

Reputation: 5101

Using password_verify in PHP

I am developing a login system for an iOS app.

This is part of the PHP script that I am using to send a JSON response to the app:

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);

My issue is about using password_verify in PHP. I want to know if it should work as it is in the script or not, then the JSON response is not received in the app.

Thank you

Upvotes: 1

Views: 115

Answers (1)

fortune
fortune

Reputation: 3372

You don't need to match password in WHERE condition, something like:

$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)) {

To prevent SQL Injection use parametrized queries ref: How can I prevent SQL injection in PHP?

Upvotes: 1

Related Questions