gpt4 book ai didi

php - PHP 和 MD5 哈希中的更改密码脚本

转载 作者:行者123 更新时间:2023-11-29 09:01:54 25 4
gpt4 key购买 nike

Possible Duplicate:
forget password page, creating a generated password to email to the user.

我正在尝试创建更改密码页面。下面的代码应该检查以确保用户位于数据库中,确认当前密码并允许他们更改密码。它会正确检查新密码,以确保它们相同且具有一定的长度,但如果用户名不在数据库中,它不会返回错误消息,并且它表示用户的当前密码位于数据库中数据库错误。我认为它与哈希密码不正确匹配,但我不确定。谁能帮我解决这些问题。谢谢。

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Change Password Confrim</title>
</head>
<body>
<?php
$db_server = "server";
$db_username = "name";
$db_password = "pass";

$con = mysql_connect($db_server, $db_username, $db_password);if (!$con)
{
die('Could not connect: ' . mysql_error());
}

$database = "User";

$er = mysql_select_db($db_username);
if (!$er)
{
print ("Error - Could not select the database");
exit;
}



//////////Collect the form data ////////////////////
$username =$_P0ST['username'];
$cur_password=$_POST['cur_password'];
$password=$_POST['password'];
$password2=$_POST['password2'];
/////////////////////////



$password = mysql_real_escape_string($password);
$password2 = mysql_real_escape_string($password2);
$username = mysql_real_escape_string($username);
$cur_password = mysql_real_escape_string($cur_password);
//Setting flags for checking
$status = "OK";
$msg="";

//Checking to see if password is at least 3 char max 8
if ( strlen($password) < 3 or strlen($password) > 15 )
{
$msg=$msg."Password must be more than 3 char legth and maximum 15 char lenght<br/>";
$status= "NOTOK";
}

//Checking to see if both passwords match
if ( $password <> $password2 )
{
$msg=$msg."Both passwords are not matching<br/>";
$status= "NOTOK";
}

$CorrectUser = mysql_query("SELECT * FROM User WHERE username ='$username' AND password = MD5('$cur_password')");
$row = mysql_fetch_array($CorrectUser);
if ($row['username'] == $username)
{
$status = "OK";
}
else {
print("Your username is not in the database. Please check that you enter the correct username and try again.");
$status = "NOTOK";
}

if ($row['cur_password'] == MD5('$cur_password'))
{
$status = "OK";
}
else
{
print("You entered the wrong current passowrd");
$status = "NOTOK";
}


if($status<>"OK"){
echo "<font face='Verdana' size='2' color=red>$msg</font><br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>";
}
else
{ // if all validations are passed.
if(mysql_query("UPDATE User SET password = MD5('$password') WHERE username ='$username'"))
{
echo "<font face='Verdana' size='2' ><center>Thanks <br> Your password changed successfully.</font></center>";
}
else
{
echo "<font face='Verdana' size='2' color=red><center>Sorry <br> Failed to change password.</font></center>";
}

}

?>
<center>
<br><br><a href='Settings.html'>Settings Page</a></center>

</body>

</html>

最佳答案

只需从 SQL 查询中删除 AND password = MD5('$cur_password') 即可。这样无论密码是否正确,您都可以找到该用户。您的 PHP 内测试 $row['cur_password'] == MD5('$cur_password') 就足够了;您不需要将其放入 SQL 查询中。

关于php - PHP 和 MD5 哈希中的更改密码脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8363637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com