如何修复php无法从mySQL数据库获取用户名和密码的问题?
我正在尝试使用mySQL数据库进行简单登录。到目前为止,我能够很好地连接到数据库,但无论出于什么原因,我都无法在我的一生中找到它为什么不接受用户名和密码的数据(现在不担心散列,这只是练习)。每次我输入它时,它只会给我错误消息,我试图使用“123”密码的用户“test”,因此我知道我没有拼写错误。除了我的错误信息,它没有说其他任何东西是错误的。这是我的登录当前的样子:如何修复php无法从mySQL数据库获取用户名和密码的问题?,php,Php,我正在尝试使用mySQL数据库进行简单登录。到目前为止,我能够很好地连接到数据库,但无论出于什么原因,我都无法在我的一生中找到它为什么不接受用户名和密码的数据(现在不担心散列,这只是练习)。每次我输入它时,它只会给我错误消息,我试图使用“123”密码的用户“test”,因此我知道我没有拼写错误。除了我的错误信息,它没有说其他任何东西是错误的。这是我的登录当前的样子: <?php //Start PHP session session_start(); require_once("db
<?php
//Start PHP session
session_start();
require_once("db_config.php");
function sanitize($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//Check if the form has been submitted
if(strtoupper($_SERVER['REQUEST_METHOD']) === 'POST') {
if (isset($_POST['pass']) && isset($_POST['user'])) {
//Predefine variables
$validationed = false;
$err_msg = "";
//Username and password has been submitted by the user
//Receive and sanitize the submitted information
$user = sanitize($_POST['user']);
$passwd = sanitize($_POST['pass']);
$user = mysqli_real_escape_string($conn, $user);
$sql = "SELECT * FROM user_logins WHERE username = '$user';";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$row_count=mysqli_num_rows($result);
//If the record is found, check password.
if ($row_count > 0) {
$row = mysqli_fetch_assoc($result);
if (password_verify($passwd, $row['pass'])) {
$validationed = true;
}
}
if ($validationed === false) {
// If the check completes without finding a valid login, set the error message.
$err_msg = "Invalid Login...Please Try Again";
} else {
// redirect to the main page of the website.
header("Location: travel.php");
exit();
}
}
}
else {
session_destroy();
session_unset();
session_start();
}
?>
<body id="LoginForm">
<div class="container">
<div class="login-form">
<div class="main-div">
<div class="panel">
<h1>Login</h1>
<p>Please enter your username and password</p>
</div>
<?php
//If $err_msg is set, display the message
if(isset($err_msg)) {
echo('<div class="alert alert-danger" role="alert">');
echo(' <strong>' . $err_msg . '</strong>');
echo('</div>');
}
?>
<form id="Login" action="login.php" method="post">
<div class="form-group">
<input type="username" class="form-control" name="user" placeholder="Username">
</div>
<div class="form-group">
<input type="password" class="form-control" name="pass" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary" name='submit'>Login</button>
</form>
</div>
</div>
</div>
</body>
使用password\u-verify
比较密码和哈希时,必须确保哈希值是使用附带的函数password\u-hash
创建的。这两个功能相辅相成
因此,要么直接比较这两个值,要么将密码以散列形式存储到数据库中(使用password\u hash
)
您确定数据库中存在您的(测试)用户吗?从用户登录中选择*的输出是什么?然后:返回的行数是0还是密码验证失败?@amain我肯定是的,输出确实显示了我的测试用户。我相信密码验证失败了,而不是返回的行,尽管我不能完全确定。希望这不是一个愚蠢的问题,但是告诉我返回的行是0会有错误吗?如果您回显$row\u count变量,它会显示0还是显示您已成功检索到一行?@DawsonIrvine谢谢!它确实告诉我,在这种情况下,我已成功返回了一行。如果您没有对密码进行哈希运算,那么您就不能使用密码\u验证
,您只需要进行一次直接的相等比较就可以了。好的!那帮我修好了,非常感谢!我不知道,所以我一定要记住!
if ($passwd == $row['pass']) {
$validationed = true;
}