尝试在php

时间:2021-03-31 19:43:01

标签: php mysqli

当我尝试在 php 中验证我的密码时,我不断收到错误“尝试访问类型 bool 中的值的数组偏移量...”。

我的密码存储在我的 MySQL 数据库中时被散列,但我在登录时无法进行 password_verify。我的代码如下:

<?php
require_once "config.php";
require_once "session.php";

$error = '';
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
$email = trim($_POST['email']);
$password = trim($_POST['password']);


// validate if email is empty
if (empty($email)) {
$error .= '<p class="error">Please enter email.</p>';
}

// validate if password is empty
if (empty($password)) {
$error .= '<p class="error">Please enter your password.</p>';
}
if (empty($error)) {
if($query = $db->prepare("SELECT * FROM users WHERE email = ?")) {
$query->bind_param('s', $email);
$query->execute();
$row = $query->fetch();

if ($row) {


if (password_verify( $password, $row['password'])) {
    
// Redirect the user to welcome page
header("location: home.php");
exit;
} else {
$error .= '<p class="error">The password is not valid.</p>';
}
} else {
$error .= '<p class="error">No User exist with that email address.</p>';
}
}
$query->close();
}
// Close connection
mysqli_close($db);
}
?>

0 个答案:

没有答案