Hashing and password_verify

泄露秘密 提交于 2019-12-02 07:50:32

you do not hash the password the user types into the form rather you hash the password when the user is actually registering into your site

$password = filter_var($_POST['aPass'] , FILTER_SANITIZE_STRING) ;
$newPassword = password_hash($password , PASSWORD_DEFAULT);
// input $newPassword into the database.

For the login process and how to use the password_verify function

$username = filter_var($_POST['username'] , FILTER_SANITIZE_STRING);
    $password = filter_var($_POST['password'],FILTER_SANITIZE_STRING);
// i assume the connection to the database has been established already
$check =  mysqli_query($con , "SELECT passwordtable FROM tablename WHERE usertable=$username") ;
if(mysqli_num_rows($check) === 1){
//fetch the assoc data,would skip that
//since the data has been fetched,we can now use the password_verify function,assuming you saved the fetched data in a variable called $dbPass

if(password_verify($password , $dbPass)){
 //the function takes in two parameters, the first being the inputted pass from your form and the second the hashed password from the database
  header('Location: dictionary.php');
  exit();
} else {
 echo 'Invalid password' ;
}

} 

You should also look at mysqli prepared statements

When you store the result of password_hash() in the database, you are storing the hashed password. To check if the inputted password is correct to log in a user, you can do something like this (pseudocode):

$result = $db->getAssoc("SELECT password FROM users WHERE username='".$username."'");
if ($result) { 
   if(password_verify($password, $result['password']){
        //log the user in
    }
}

http://php.net/manual/en/function.password-verify.php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!