首页 > 解决方案 > password_verify() 函数被警告为未定义的常量

问题描述

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

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

它成功地在我的数据库中保存了一个散列密码,例如:$2y$10$.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");
    }
  }
}

?>

标签: phphashcode

解决方案


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

第一个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
}

您还应该更改为准备好的 statements,因为这现在可能容易受到 sql 注入的影响。


推荐阅读