我正在为我的网站创建更改密码网站,我的代码存在一些问题......
由于某种原因,我在密码被比较后在数据库中进行比较和替换时遇到了困难。
我想要这个:
获取当前用户密码并将其与$ oldpass的输入值进行比较,或将$ oldpass的输入值与当前用户存储在数据库中的密码进行比较。
检查$ oldpass和数据库中的密码是否匹配,如果它们匹配,则取$ newpass和$ repeatpass的输入值,比较它们,如果它们匹配,则使用crypt()$ newpass并更新数据库新密码。
我甚至不确定密码是否被加密。
同样在代码中我将$ oldpass与$ _SESSION ['password']进行比较,这不是db的密码,我无法弄清楚如何从db调用密码。
<?php
include 'check_login_status.php';
$u="";
$oldpass=md5($_POST['oldpass']);
//stripping both strings of white spaces
$newpass = preg_replace('#[^a-z0-9]#i', '', $_POST['newpass']);
$repeatpass = preg_replace('#[^a-z0-9]#i', '', $_POST['repeatpass']);
//get the username from the header
if(isset($_GET["u"])){
$u = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
} else {
header("location: compare_pass.php?u=".$_SESSION["username"]);
exit();
}
// Select the member from the users table
$sql = "SELECT password FROM users WHERE username='$u' LIMIT 1";
mysqli_query($db_conx, $sql);
$user_query = mysqli_query($db_conx, $sql);
// Now make sure that user exists in the table
$numrows = mysqli_num_rows($user_query);
if($numrows < 1){
echo "That user does not exist or is not yet activated, press back";
exit();
}
if ($oldpass == $_SESSION['password']) {
echo "session and oldpass are matching";
} else {
echo "Session and oldpass do not match!";
}
$isOwner = "no";
//check if user is logged in owner of account
if($u == $log_username && $user_ok == true){
$isOwner = "yes";
}
$newpass = password_hash($newpass, PASSWORD_BCRYPT);
if (isset($_POST["submit"]) && ($isOwner == "yes") && ($user_ok == true) && ($newpass == $repeatpass)) {
$newpass = password_hash($newpass, PASSWORD_BCRYPT);
$sql = "UPDATE users SET `password`='$newpass' WHERE username='$u' LIMIT 1";
}
if (mysqli_query($db_conx, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($db_conx);
}
?>
<h3>Create new password</h3>
<form action="" method="post">
<div>Current Password</div>
<input type="text" class="form-control" id="password" name="oldpass" ><?php echo "{$oldpass}"; ?>
<div>New Password</div>
<input type="text" class="form-control" id="password" name="newpass" ><?php echo "{$newpass}"; ?>
<div>Repeat Password</div>
<input type="text" class="form-control" id="password" name="repeatpass" ><?php echo "{$repeatpass}"; ?>
<br /><br />
<input type="submit" name="submit" value="Submit">
<p id="status" ></p>
</form><?php echo "{$oldpass}, {$_SESSION['password']}"; ?>
<pre>
<?php
var_dump($_SESSION);
var_dump($oldpass);
var_dump($newpass);
var_dump($repeatpass);
?>
</pre>
答案 0 :(得分:0)
Ther是解决问题的一种更简单的方法:
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);
// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);
算法MD5不是保护密码的好选择,因为它的设计速度很快,而且很容易被强制使用。
在会话中存储密码/哈希不是很有帮助,如果您知道它是同一个用户,您知道他是否已经登录,只需在会话中存储指示符,如$ _SESSION [&#39; is_logged_in& #39;]或只是用户名$ _SESSION [&#39;用户名&#39;]。