Using password_hash with bindParam

I am trying to create a login system using Slim Jquery and Ajax. I have part of the log working with minimal problems, now I just need to have a hash password. I know I can use md5, sha1 and / or salt for the hash, but I know that password_hash is being used again. I know how to hash with any of the other 3 I mentioned because using bindParam you can just wrap it around a variable. My question is how can I use password_hash with bindParam. The closest answer I found on this site didn't help.

My current code:

$app->post('/addUser/', 'addUser');
function addUser()
{
    $request = \Slim\Slim::getInstance()->request();
    $q = json_decode($request->getBody());

    $sql = "INSERT INTO users(firstName, lastName, userName, password) VALUES (:firstName, :lastName, :userName, :password)";

    try{
        $dbConnection();
        $stmt=$db->prepare($sql);
        $stmt->bindParam("firstName", $q->firstName);
        $stmt->bindParam("lastName", $q->lastName);
        $stmt->bindParam("userName", $q->userName);
        $stmt->bindParam("password", $q->password);
        $stmt->execute();
        $db=null;
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }
}

      

Check code:

$app->post('/logIn/', 'lonIn');
function logIn()
{
    $request = \Slim\Slim::getInstance()->request();
    $q = json_decode($request->getBody());

    $sql = "SELECT * FROM users WHERE userName=:userName";
    try{
        $db = getConnection();
        $stmt=$db->prepare($sql);
        $stmt->bindParam("userName", $q->userName);
        $execute = $stmt->execute();
        $db = null;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
    if($execute == true)
    {
        $array = $stmt->fetch(PDO::FETCH_ASSOC);
        $hashedPassword = $array['password'];
        if(password_verify($q->password), $hashedPassword))
        {
            echo 'Valid';
        }
        else
        {
            echo 'Invalid';
        }
    }
}

      

Any help would be appreciated.

+3


source to share


1 answer


To encrypt the password, you need to create a new $ hashedPassword variable that you will store in the db for each user. When validating a user, you will select a user from the db passing in their username and using password_verify ($ passToBeVerified, $ ourHashedpasswordfromDb) this will return a boolean.



      $app->post('/addUser/', 'addUser');

function addUser() {
    $request = \Slim\Slim::getInstance()->request();
    $q = json_decode($request->getBody());
    $hashedPassword = password_hash($q->password, PASSWORD_BCRYPT);

    $sql = "INSERT INTO users(firstName, lastName, userName, password) VALUES (:firstName, :lastName, :userName, :password)";

    try {
        $dbConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam(":firstName", $q->firstName);
        $stmt->bindParam(":lastName", $q->lastName);
        $stmt->bindParam(":userName", $q->userName);
        $stmt->bindParam(":password", $hashedPassword);
        $execute = $stmt->execute();
        if ($execute == true) {
            $verifyUser = verifyUser($q->password, $q->userName);
            if ($verifyUser == TRUE) {
                echo 'valid Username and  Password';
            } else {
                echo 'Invalid Username and password';
            }
        }
        $db = null;
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

function verifyUser($passWordToVerify, $userNameToVerify) {
    // $request = \Slim\Slim::getInstance()->request();
    //   $q = json_decode($request->getBody());
    //Select a user data according to their username
    $sql = "select firstName, lastName, userName, password from users where userName = :userName";
    try {
        $dbConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam(":userName", $userNameToVerify);
        $execute = $stmt->execute();
        $db = null;
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
    if ($execute == True) {
        /*
         * if the query executes and returs the user saved user details lets now compare
         * the password from the db and the password that the user has entered
         */
        $array = $stmt->fetch(PDO::FETCH_ASSOC);
        $hashedPassword = $array['password'];
        if (password_verify($passWordToVerify, $hashedPassword)) {
            echo 'Password is valid!';
            return true;
        } else {
            echo 'Invalid password.';
            return false;
        }
    }
}

      

+2


source







All Articles