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

时间:2020-09-27 06:48:37

标签: php authentication mysqli registration

<?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:\ xampp \ htdocs \ defgonnawork \ login.php中访问类型为bool的值的数组偏移量

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

1 个答案:

答案 0 :(得分:0)

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

$result = $query->get_result();

/* now you can fetch the results into an array */

if($row = $result->fetch_assoc()){

    //rest of the code

}
相关问题