【发布时间】:2017-09-30 08:29:04
【问题描述】:
我是PHP 和MySQL 的初学者。我正在尝试验证 MySQL 5.7 DB 中的用户输入(用户名和密码),但没有机会。让我解释一下我的问题;
我在firstdb 中创建了一个用户,用户名:firstuser 密码:firstpassword 通过phpMyAdmin 页面。
代码(check_user-pass.php)
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="rootpassword"; // Mysql password
$db_name="firstdb"; // Database name
$tbl_name="mysql.user"; // Table name
$conn = new mysqli($host, $username, $password, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// Define $username and $password
$myusername=$_POST['EMail'];
$mypassword=$_POST['Password'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = mysqli_real_escape_string($conn,$_POST['EMail']);
$mypassword = mysqli_real_escape_string($conn,$_POST['Password']);
$sql="SELECT * FROM $tbl_name WHERE user='$myusername' and authentication_string='$mypassword'";
$result = $conn->query($sql);
$count = 0;
// Mysql_num_row is counting table row
if ($result = mysqli_query($conn, $sql)) {
/* determine number of rows result set */
$count = mysqli_num_rows($result);
printf("Result set has %d rows.\n", $count);
/* close result set */
mysqli_free_result($result);
}
// If result matched $username and $password, table row must be 1 row
if ($count==1) {
echo "Success! $count";
} else {
echo "Unsuccessful! $count";
}
ob_end_flush();
?>
代码返回: Connected successfullyResult set has 0 rows. Unsuccessful! 0
如果我删除此行:and authentication_string='$mypassword'代码返回: Connected successfullyResult set has 1 rows. Success! 1
我成功地从index.php 检索到check_user-pass.php 的用户输入,但mysql.user 表中没有用于在数据库中匹配的明确密码列。
我在网上搜索并找到; password 列在 5.x 中更改为 authentication_string 但此列带有散列值。所以我无法将用户密码与此匹配。
问题 1
我应该创建一个表并将每个用户的用户名和密码存储在数据库中以进行验证吗?
问题 2
如果question1的答案是NO,如何实现这个验证问题?
【问题讨论】:
-
存储 [...] 清除密码 - 不!在任何情况下都不要存储清晰的密码。
-
想也一样。
-
不建议转义密码,尤其是当
123'\abc完全有效时。在转义这些时,它会将其重写为123\'\abc反过来在password_verify()上失败,如果并且当您使用password_hash()存储散列密码时。在任一情况下;它仍然会失败。