Php 无法与数据库中的md5密码进行比较
signup.phpPhp 无法与数据库中的md5密码进行比较,php,sql,md5,password-encryption,Php,Sql,Md5,Password Encryption,signup.php $password_unencrypted = $_POST['passwd']; $password=md5($password_unencrypted); $query = "INSERT INTO Customers (firstname, lastname, username, password, " . "gender,mobile,email) " . "VALUES ('$first_name', '$last_name', '$user_
$password_unencrypted = $_POST['passwd'];
$password=md5($password_unencrypted);
$query = "INSERT INTO Customers (firstname, lastname, username, password, " . "gender,mobile,email) " . "VALUES ('$first_name', '$last_name', '$user_name', '$password', " . " '$gender','$mobile','$email')";
Login.php
$username=$_POST['username'];
$password=md5($_POST['password']);
$sql = ("select * from Customers where username='".$username."' and password='".$password."'") or die('Error connecting to MySQL server.');
$query = mysqli_query($con,$sql);
$result=mysqli_fetch_row($query);
if($result)
{
$_SESSION['username']=$username;
header('location:home.html');
}
else
{
echo md5($_POST['password']);
echo 'Your entered username or password is incorrect';
}
在上面的注册和登录代码中,我将md5应用于密码存储
我签入了数据库,md5密码存储正确,但检索不正确(我想)
尝试登录页面失败
仅供参考:echo md5($_POST['password']);在Login.php中显示了存储在数据库中的相同密码下面是如何修复Login.php代码 您的检查完全错误您需要首先检查查询是否成功运行,然后检查返回的行是否超过0,这意味着用户名正确,我们将继续进行密码检查如果一切正常,我们将启动会话,假设您的页面顶部有session_start(),如果没有在
$\u会话之前添加它['username']=$username;
检查手册中的和
在register.php上修改将密码保存到数据库中
$password=md5($\u POST['password']);
到$password=password\u散列($\u POST['password'],password\u默认);
请使用PHP()。如果您使用的PHP版本低于5.5,则可以使用密码\u hash()。您还应该使用带有占位符的参数化查询,而不是将变量直接连接到查询字符串中。建议阅读:1.不要使用MD5或SHA-1,使用SHA-256之类的SHA-2函数。2.不要使用静态salt,每个使用相同密码的条目都将具有相同的salt,因此当发现一个时,所有其他条目都将被删除同样公开的是,使用也保存在DB中的随机salt。3.不要仅仅连接salt,使用HMAC函数。4.由于计算哈希非常快,因此有必要迭代哈希函数。5.使用标准密码派生函数,如Crypto.HashPassword
,PBKDF2
,password\u hash
,<代码> BCRIPT < /代码>、脚本等。考虑到您的用户有来自密码处理不当的风险,他们期望并值得获得良好的安全性。请参阅安全STACKExchange。请参见OWASP(Open Web应用安全项目)。
<?php
if isset($_POST['submit']) {
$username= mysqli_real_escape_string($con, trim($_POST['username']));
$password = trim($_POST['password']); // no need to sanitize the password
$sql = "select * from Customers where username = '" .$username."' "; // you don't need or Die() it's just a string
if ($result = mysqli_query($con,$sql)) //check if the Query succeeded running
{
$count = mysqli_num_rows($result);
if($count > 0 )
{ // if username exists we proceed to checking password
$fetch = mysqli_fetch_assoc($result);
$hashedpassword = $fetch["password"];
if ( password_verify($password, $hashedpassword) )
{ //checking password
$_SESSION['username']=$username;
header('location:home.html');
exit;
}else {
echo "incorrect username or password"; // you don't want to tell him that the username is fine but the password is not correct
}
} else {
echo "incorrect username or password";
}
} else {
echo 'Query failed to run';
}
}
?>