密码哈希不适用于mysql

xam8gpfp  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(349)

我在这里呆了一整天,看了所有的答案,也看了网上关于密码散列的教程。似乎我做得对,它散列了数据库中的密码,但当我去登录它告诉我,密码是不正确的。这是剧本,任何帮助都将不胜感激。
登录.php

if (isset($_POST['username'])){
//escapes special characters in a string
$username = stripslashes($_REQUEST['username']);
$username = mysqli_real_escape_string($con,$username);
$password = $_REQUEST['password'];
$password = mysqli_real_escape_string($con,$password);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE username='$username'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$hash = $row['password'];
if (password_verify($password, $hash)) {
    $_SESSION['username'] = $username;
    // Redirect user to index.php
    $position = $row['position'];
    if ( $position == "freelancer" ){
    header("Location: ../freelancer/index.php");
    }
    elseif ($position == "employer"){
    header("Location: ../employer/index.php");
    }
}
else{
echo "<div class='form'>
<h3>Username/password is incorrect.</h3>
".$hash." " .$password. "
<br />Click here to <a href='login.php'>Login</a>
</div>";

register.php(散列密码的代码)

//BEGINNING CODE HERE
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
$hashPassword = password_hash($password, PASSWORD_DEFAULT);
$sql_u = "SELECT username FROM users WHERE username='$username'";
$sql_e = "SELECT email FROM users WHERE email='$email'";
$res_u = mysqli_query($con, $sql_u);
$res_e = mysqli_query($con, $sql_e);
if (mysqli_num_rows($res_u) > 0) {
$name_error = "Sorry... username already taken";
}
else if(mysqli_num_rows($res_e) > 0){
$email_error = "Sorry... email already taken";
}
else{
$query = "INSERT INTO users (username, email, password)
VALUES ('$username', '$email', '$hashPassword')";
$results = mysqli_query($con, $query);
mkdir("../profilePics/" . $username);
echo 'You Have Registered Successfully!<br>
To Login Go To <a href="login.php">Our Login Page</a>
';
exit();
}
}
?>

我知道这段代码可能会有更多的错误,但我只对基本的密码哈希理解感兴趣。

3bygqnnd

3bygqnnd1#

$password = mysqli_real_escape_string($con,$password);

您的密码现在已转义,它肯定与哈希不匹配。最好去掉这根线,找一个更好的方法来确保绳子是安全的。
逃避意味着什么?如果您的密码包含“它将变成”
我应该用mysql\u real\u escape\u string在注册表中输入密码吗?

相关问题