password_verify() 函数被警告为未定义常量

password_verify() function warned as undefined constant

我最近使用了一种登录格式,它使用 $password = md5(password_1) 下面的完整代码将密码存储在数据库中...

然后将其更改为 $password = (password_hash($password_1, PASSWORD_DEFAULT); 下面的完整代码....

它成功地在我的数据库中保存了一个散列密码,如:y$.FTJmF/47NbmQMU3nZGTZeKAHYZ8TBm8X2Jc.TLbAIK...

现在验证密码是出了问题的地方, 原始代码是 $password = md5($password_1) ,它将使用存储在第一个代码中的 md5 成功登录。 我将其更改为 $password = password_verify($password_1, PASSWORD_DEFUALT);

但这就是给我这个错误。

我试过了$hash = password_verify($password_1, PASSWORD_DEFAULT ); 但它给了我同样的错误,

 <?php

session_start();

// initializing variables
$username = "";
$email    = "";
$errors = array(); 

// connect to the database
$db = mysqli_connect('', '', '', '');

// REGISTER USER
if (isset($_POST['reg_user'])) {
  // receive all input values from the form
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($username)) { array_push($errors, "Username is required"); }
  if (empty($email)) { array_push($errors, "Email is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
  }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM loginsystem WHERE username='$username' OR email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
  //Hashing the password
  $password = password_hash($password_1, PASSWORD_DEFAULT);//encrypt the password before saving in the database

    $query = "INSERT INTO loginsystem (username, email, password) 
              VALUES('$username', '$email', '$password')";
    mysqli_query($db, $query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";
    header('location: home.php');
  }
}

// ... 
// ... 

// LOGIN USER
if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password_1']);

  if (empty($username)) {
    array_push($errors, "Username is required");
  }
  if (empty($password)) {
    array_push($errors, "Password is required");
  }

  if (count($errors) == 0)  $password = password_verify($password, PASSWORD_DEFAULT); {
    $query = "SELECT * FROM loginsystem WHERE username='$username' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      $_SESSION['username'] = $username;
      $_SESSION['success'] = "You are now logged in";
      header('location: index.php');
    }else {
        array_push($errors, "Wrong username/password combination");
    }
  }
}

?>

PASSWORD_DEFUALT 拼写错误。它应该是 PASSWORD_DEFAULT,如下例所示:

$hash = password_hash($password, PASSWORD_DEFAULT);

[...]

if (password_verify($password, $hash)) {
    // Password matches
}

password_verify的第一个参数是用户输入的密码。第二个参数是password_hash.

生成的存储哈希

除了错别字还有两个问题:

1st: password_verify() 有不同的参数:输入的密码和来自数据库的散列密码。

这导致我们进入第二个问题
您只能在从数据库中获取哈希后才能验证密码。 因此,首先获取该哈希值(通过单独查询用户名),然后进行验证。

$query = "SELECT username, email, password FROM loginsystem WHERE username='$username'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
    $row = mysqli_fetch_assoc($results);
    if(password_verify($password, $row['password'])) {
        // password correct!
    } else {
        // nope
    }
} else {
    // wrong credentials
}

您还应该更改为 prepared statements,因为这现在可能容易受到 sql 注入攻击。