我有以下代码注册,它哈希密码,我可以在数据库中看到成功哈希
function newUser() {
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$securityq = $_POST['securityq'];
$securitya = $_POST['securitya'];
$password = $_POST['password'];
$hash = password_hash ($password, PASSWORD_BCRYPT );
$query = "INSERT INTO admin (forename,surname,email,securityq, securitya,password) VALUES ('$forename','$surname','$email','$securityq','$securitya','$hash')";
$data = mysql_query ($query)or die(mysql_error());
if($data)
{
}
}
以下代码适用于我的登录页面:
$email = $_POST['email'];
$password = $_POST['pass'];
function SignIn()
{
session_start();
if (!empty($_POST['email']))
{
$query = mysql_query ("SELECT * FROM admin where email = '$_POST[email]' AND password = '$_POST[pass]'");
$row = mysql_fetch_array ($query);
if(!empty($row['email']) AND !empty($row['password']))
{
$_SESSION['email'] = $row['password'];
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Successful Login')
window.location.href='adminhome.php';
</SCRIPT>");
}
else
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Invalid Login Credentials')
window.location.href='adminsignin.php';
</SCRIPT>");
}
}
}
if(isset($_POST['submit']))
{
SignIn();
}
但我无法使用密码登录。 我已经使用登录代码摆弄,但无法找到我需要更改以解决此问题的方法, 非常感谢,提前。
答案 0 :(得分:4)
您永远不会对登录表单提供的密码进行哈希处理,因此您正在执行
if (real password == hashed string)
永远不会匹配。你需要更像
的东西$hash = password_hash ($_POST['password'], PASSWORD_BCRYPT );
$sql = "SELECT ... WHERE hash='$hash'";
当然,您有大量开放的SQL injection attack漏洞,因此您的登录表单完全没用。
答案 1 :(得分:0)
使用SQL语句搜索散列密码是不可能的,因为散列是哈希的。相反,您可以从数据库中读取哈希值,然后检查哈希值是否与提供的密码匹配:
SELECT password FROM admin where email = ?
使用数据库中的此哈希,您可以验证密码:
$isPasswordCorrect = password_verify($_POST[pass], $existingHashFromDb);