php - 散列密码 PHP mySQLI

标签 php mysql

<分区>

所以我试图让用户输入的密码在存储在我的数据库中时被加密。使用 BCRYPT 我可以做到这一点,但是现在加密存储在数据库中,用户无法使用他们选择的密码登录。 有没有人有任何建议我将如何继续这件事?

感谢任何形式的帮助!

提前致谢!

Register.php page below

<?php
require 'C:\wamp\www/projekt/connections.php';

if(isset($_POST['submit'])) {


session_start();
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$uname = $_POST['username'];
$pwd = $_POST['password'];

$hashedpassword = password_hash($pwd, PASSWORD_DEFAULT);



$sql = $con->query("INSERT INTO users (FirstName, LastName, UserName,   Password)VALUES('{$fname}', '{$lname}', '{$uname}', '{$hashedpassword}')");

if (password_verify($pwd, $hashedpassword)) {


 header('Location: login.php'); 
 }



 }

 ?>

----------------------------------------------------------

login.php page below

<?php

$con = mysqli_connect("localhost","root","","userreg");

if(isset($_POST['login'])){

$uname = mysqli_real_escape_string($con,$_POST['Username']);

$pwd = mysqli_real_escape_string($con,$_POST['Password']);

$sel_user = "select * from users where UserName='$uname' AND Password='$pwd'";

$run_user = mysqli_query($con, $sel_user);

$check_user = mysqli_num_rows($run_user);

if($check_user>0){

$_SESSION['UserName']=$uname;

echo "<script>window.open('startpage.php','_self')</script>";

}

else {

echo "<script>alert('Username or password is not correct, try again!')</script>";



}

}

?>

最佳答案

您可以使用 php 的 password_hash 和 password_verify。它对密码进行哈希处理和加盐。

//Store $hashedPassword in the database under the password column.
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);

//Or you could use PASSWORD_DEFAULT over PASSWORD_BCRYPT which will default to php's current default method. 
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);


//Query the database and pull out the hashed password.
//Pass the user entered password and the retrieved/stored hash into the password_verify method. 
//If it is a match, it will return true.
if (password_verify($password, $hashedPassword)) {
   // Correct password
}

编辑:这里是散列/存储/验证密码的流程。

(创建新用户 - 密码)

  1. 获取用户输入(确保清除所有用户输入/使用准备好的语句!看看 PDO/MySQLI)

  2. 散列密码。 $hashedPassword = password_hash($password, PASSWORD_DEFAULT);参数$password是用户输入。

  3. 将新变量/哈希密码 $hashedPassword 存储到您的数据库中。

至此,一个用户已经创建,并且他们的密码/其他信息已经存储到数据库中。

(登录用户)

  1. 获取用户输入(确保清除所有用户输入/使用准备好的语句!看看 PDO/MySQLI)

  2. 查询您的数据库并检索用户密码(从用户名/id 等于他们输入的数据库中选择密码)。

  3. 将步骤 1 中的用户输入和步骤 2 中检索到的密码传递给方法:password_verify($password, $hashedPassword) - $password是用户输入,$hashedPassword 是我们从数据库中提取的密码。如果密码匹配,此方法将返回 true,否则返回 false。

-

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

 // Correct password
 //Set the session variables/whatever else you would like to do now that we have verified that the user has the correct password.

} else {

 //Redirect the user/inform them that they have the incorrect username/password combination.

}

关于php - 散列密码 PHP mySQLI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35782911/

相关文章:

php - 获取与表中另一个 ID 相关的 ID,然后使用该 ID 从另一个表中获取信息

javascript - 展开连接查询中的嵌套数据

php - 如果表单上出现错误,则在提交后显示下拉列表的多个选定值

mysql - 尝试从一个表中删除另一个表中存在的记录

mysql - 根据 MySQl 中的计数更新重复列值

php - 获取一列中相同日期的数据另一列中的另一个日期数据

php - 如何使用市场多供应商扩展在 magento 中的销售订单网格中自定义列

php - 是否有用于处理单位和数量的 PHP 库/类?

php - MySQL ORDER BY IN()

php - 如何将多维 PHP 数组转换为 XML?