例如,有人尝试使用名称“Bob”登录,但数据库中没有 Bob。是否应该通知用户没有 Bob,或者程序是否应该简单地说“身份验证失败”(我注意到 Gmail 会这样做)?这部分是可用性问题,部分是效率问题。现在程序查询数据库以查看给定的用户名是否存在,如果存在,则再次查询数据库以查找相同用户名的密码哈希(冗余)。
//$link is the link to the database storing passwords/usernames
if(userNameExists($uName, $link))
{
if(passwordCorrect($uName, $pass, $link))
echo 'log in successful!';
else
echo 'can\'t log in';
}
else
{
echo 'username doesn\'t exist!';
}
/*This function checks to see if the username exists
INPUT: the userName to check for and a link to the database
OUTPUT: true if username exists
*/
function userNameExists($userName, $link)
{
$result = mysqli_query($link, 'SELECT `userid`
FROM `login`
WHERE `userid` = \''.$userName.'\' LIMIT 1');//need to look into uses of backticks, single quotes, double quotes
return mysqli_num_rows($result) == 1;
}
/*This function checks the password for a given username
INPUT: the userName and password the user entered, and a link to the database
OUTPUT: true if the given password matches the one in the database
*/
function passwordCorrect($userName, $givenPassword, $link)
{
$result = mysqli_query($link, 'SELECT `password`
FROM `login`
WHERE userid = \''.$userName.'\' LIMIT 1');
$retrievedPassword = mysqli_fetch_array($result);
if(password_verify($givenPassword, $retrievedPassword['password']))
return true;
else
return false;
}
我应该只使用passwordCorrect()
并且如果mysqli_query()
返回 false 这意味着用户名不存在(诚然我不喜欢这些解决方案,因为这可能意味着其他地方出了问题,不是吗?)?