用PHP进行了登录和注册,登录时说密码无效


<?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 an 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, isset($row['password']))) {
$_SESSION["userid"] = $row['id'];

// redirect the user to the welcome page
header("location: index.php");
exit;
} else {
$error .= '<p class="error">The password is not valid.</p>';
}
} else {
$error .= '<p class="error">No user exists by that email address.</p>';
}
}
$query->close();
}
// close connection
mysqli_close($db);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">


<form action="" method="post">
<h2>Login</h2>
<?php echo $error; ?>
<div class="form-group">
<label>Email Address</label>
<input type="email" name="email" class="form-control" required />
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" required />
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary" value="submit">
</div>
</form>
</div>
</div>
</body>
</html>

我犯过不少错误,比如if (password_verify($password, $row['password']))说:

注意:试图访问第22行C:\examplep\htdocs\defgonnawork\login.php中bool类型值的数组偏移量

但我刚刚在那里扔了一个isset数据库连接正确,使用电子邮件和密码注册成功,我可以看到它保存在数据库中。

您没有得到结果。试试这个

$result = $query->get_result();
/* now you can fetch the results into an array */
if($row = $result->fetch_assoc()){
//rest of the code
}

最新更新