我对我的登录脚本有疑问。用户存在时登录错误
我已经使用PHP,看起来像这样登录脚本:
<?php
include 'headermet.php';
include 'database.php';
session_start();
if(isset($_SESSION['user_id'])){
header("Location: /");
}
if(!empty($_POST['username']) && !empty($_POST['password'])):
$records = $conn->prepare('SELECT id,username,password FROM users WHERE username = :username');
$records->bindParam(':username', $_POST['username']);
$records->execute();
$results = $records->fetch(PDO::FETCH_ASSOC);
$message = '';
if(count($results) > 0 && password_verify($_POST['password'], $results['password'])){
$_SESSION['user_id'] = $results['id'];
header("Location: /");
} else {
$message = 'ERROR while trying to login';
}
endif;
?>
<body>
<h1 style = "margin-top:100px;">LOGIN</h1>
<?php if(!empty($message)): ?>
<p><?= $message ?></p>
<?php endif; ?>
<form action="login.php"= method="POST">
<input type="text" placeholder="Gebruikersnaam" name="username">
<input type="password" placeholder="Wachtwoord" name="password">
<input type="submit">
</form>
</body>
<?php include 'footer.php';?>
在我添加了一个错误消息,如果凭据不正确的代码。该消息是$message = 'ERROR while trying to login';
使用sequelpro我加入的用户名测试用户:测试密码:测试
好然后做一些调试,找出它出错...是否'$结果[ '密码']'包含的值? 'count($ results)'实际上更大的零? (顺便说一句,你正试图在你做这个检查的时候获取一个结果行 - 没什么意义。)'password_verify'的返回值是多少? ...你知道,找出为什么我的剧本做它做的绝对的基础... – CBroe
你的php代码是好的。检查你的数据库表或数据库连接 –