PHP password_verify()不起作用

时间:2017-02-05 21:42:05

标签: php mysql

它始终提供错误的密码。数据库中的密码部分设置为VARCHAR(256),这不是问题,如果我尝试strlen($ row ['password']),它将返回60.请帮助。

的login.php

<html>

<form action="loginHandler.php" method="post">

Username: <input type="text" name="usernameLogin" value=""><br>

Password: <input type="password" name="passwordLogin" value=""><br>

<input type="submit" value="Submit" name="submitLogin" />


</html>

loginHandler.php

<?php
if(isset($_POST['submitLogin']))
{
  $usernameLogin = ($_POST['usernameLogin']);
  $passwordLogin = ($_POST['passwordLogin']);
}

require("connectDB.php");

$dbname = "3year";
mysqli_select_db($conn, $dbname);

$sql = "SELECT username, password FROM user_info WHERE username = '$usernameLogin'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);

$count = mysqli_num_rows($result);

if($count == 1)
{
    if(password_verify($passwordLogin ,$row['password']))
        echo "Login successful!";
    else
        echo "Incorrect password!";
}
else
    echo "Username not valid!";


$conn->close();

?>

在数据库中插入密码。

<?php
if(isset($_POST['submit']))
{
  $username = $_POST['username'];
  $password = $_POST['password'];
  $mails = $_POST['email'];
}
else echo "Not set!";

echo $username;
require("connectDB.php");

$dbname = "3year";
mysqli_select_db($conn, $dbname);

$hash = password_hash($password, PASSWORD_DEFAULT);

$sql = "INSERT INTO user_info (username, password, mails)
VALUES ('$username', '$hash', '$mails')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();


?>

0 个答案:

没有答案