【发布时间】:2020-06-18 18:25:20
【问题描述】:
我遇到了一些 MySQL 问题并且非常困惑,因为这似乎不可能。无论如何,当我尝试登录时,当我输入正确的密码时,我会被发送到显示用户名/密码不正确的屏幕。
基本上,我的数据库如下所示:
USERNAME PASSWORD EMAIL
Xp10d3 Password12345 xp10d3@gmail.com
IiBlurBeriI Password33333 iiblurberii@gmail.com
我想检查用户是否输入了他们的用户名以及密码是否匹配,但我对如何做到这一点感到困惑。我在编写代码时得到了一些帮助,但它似乎不起作用,因为可以在验证之前输入任何用户名(在用户的电子邮件中验证)并且一旦他们输入密码就不必匹配。 前任。 我想要的是: 用户输入用户名 Xp10d3。用户输入密码Password1111。有一条错误消息说密码不匹配。然后用户输入密码Password12345。它是有效的并将 $_SESSION 变量设置为他们的用户名。 现在发生了什么: 用户输入数据库中的 Xp10d3。他们输入的密码是正确的 Password12345,然后他们会被发送到显示密码错误的屏幕。
我做了一些测试并记录了所有变量。这一切都归结为$row["PASSWORD"] 不正确的事实。我该如何解决这个问题?
一些重要说明:
变量$_SESSION['password_not'] 不是散列密码。但是,除了那个变量和$password 变量之外,我得到的所有其他变量都被散列了。
未散列的变量列表:
$_SESSION['password_not']
$password
经过哈希处理的变量:
$row["PASSWORD"]
Any passwords in the database
代码: login_check_update.php:
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
text-align: center;
font-family: sans-serif;
}
a {
text-decoration: none;
color: blue;
}
#logout {
margin: 0 auto;
text-align: center;
border: 1px solid;
border-radius: 5px;
max-width:1024px;;
height: 800px;
}
</style>
</head>
<body>
<div id="logout">
<?php
session_start();
/* Sends an email to the user and adds the special key to another database */
$username = $_GET['username']; /* Gets the username that was submitted in the HTML form. */
$password = $_GET['password']; /* Gets the password that was submitted in the HTML form. */
$servername = "localhost"; /* MySQL database. Change if needed! Most of the time its not localhost unless you're hosting on your computer. */
$user = 'xxx'; /* MySQL username. Change if needed. */
$pass = 'xxx'; /* MySQL password. Change if needed. */
$dbname = 'vibemcform'; /* MySQL database name. Change if needed. */
$bytes = random_bytes(10); /* Randomized code */
$key = bin2hex($bytes); /* Makes the randomized code */
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$link = "live.php";
$con = new mysqli($servername, $user, $pass, $dbname); /* Connects to the database */
$query = "SELECT USERNAME FROM data WHERE USERNAME = '".$username."'";
$result = $con->query($query);
$row = $result->fetch_assoc();
$selectTwo = "SELECT PASSWORD FROM data WHERE PASSWORD = '".$password."'";
$result2 = $con->query($selectTwo);
$row2 = $result->fetch_assoc();
/* Delete after */
$test = $row["USERNAME"];
$testTwo = password_verify($_SESSION['password_not'], $row["PASSWORD"]);
if ($username == $row["USERNAME"] && password_verify($_SESSION['password_not'], $row["PASSWORD"])) {
echo "Found data in the database! Visit the chat!";
echo "<form action='live.php' method='post'><a href='".$link."'><input type='submit' name='btn1' value='$username'/></a></form>";
echo "Session ID: ". session_id() . ". ";
} else {
echo "Username not found/password incorrect. Please try again!";
}
$conn = null;
exit;
?>
<a href="index.html">Home</a>
</div>
</body>
</html>
【问题讨论】:
-
为什么您的数据库中的密码没有经过哈希处理?
-
您不能对纯文本密码使用
password_verify()。您必须先使用password_hash()对它们进行哈希处理。 -
不要在会话中存储密码,这不是一个好主意。
-
这段代码是从哪里来的?这绝对是不可以使用的东西。
-
警告:使用
mysqli时,您应该使用parameterized queries 和bind_param将任何数据添加到您的查询中。 请勿使用字符串插值或连接来完成此操作,因为您创建了严重的SQL injection bug。 切勿将$_POST、$_GET或任何类型的数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。
标签: php mysql session passwords