2016-03-03 106 views
0

所以即时通讯设法使用户输入的密码在我的数据库中存储时将被加密。 BCRYPT能够做到这一点,但是现在将加密存储在数据库中,用户无法使用他们选择的密码登录。 有没有人有任何建议,我会怎么继续呢?散列密码PHP mySQLI

感谢任何帮助!

在此先感谢!

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



} 

} 

?> 
+0

结帐在我的岗位的编辑。 – Son

回答

2

您可以使用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步到方法的retreieved密码: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. 

} 
+0

感谢您的回答!但即时通讯仍在挣扎中,您是否可以详细阐述一下?在编程方面,我相当新,在PHP及其功能方面真的很新颖。我试着用PASSWORD_DEFAULT来代替,并且在查询之后我有if语句。我觉得我有些误会。 – Vanhorn

+0

没有问题 - 你能用你当前的代码更新你的文章吗? – Son

+0

完成!我觉得你的评论“将用户输入的密码和检索/存储的散列传递给password_verify方法。”是我没有正确完成/根本吗? – Vanhorn