【问题标题】:PHP crypt(), UPDATE and ComparisonsPHP crypt(),更新和比较
【发布时间】:2015-02-20 11:23:50
【问题描述】:

我正在为我的网站创建更改密码网站,但我的代码有一些问题...

由于某种原因,我在加密密码后在数据库中比较和替换密码时遇到了困难。

我想要这个:

获取当前用户密码并将其与 $oldpass 的输入值进行比较,或者将 $oldpass 的输入值与当前用户存储在数据库中的密码进行比较。

在检查 $oldpass 和数据库中的密码是否匹配并且如果它们匹配后,获取 $newpass 和 $repeatpass 的输入值,比较它们,如果它们匹配,然后 crypt() $newpass 并使用更新数据库新密码。

我什至不确定密码是否已加密。

另外在代码中,我将 $oldpass 与 $_SESSION['password'] 进行比较,这不是数据库中的密码,我不知道如何从数据库中调用密码。

    <?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>

【问题讨论】:

标签: php mysql session


【解决方案1】:

有一个更简单的方法来解决这个问题:

// 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['is_logged_in'] 或只是用户名 $_SESSION['username']。

【讨论】:

  • 伟大的 crypt 功能现在可以工作了。但是我仍然很难弄清楚为什么我的代码没有检索到已经存储在数据库中的密码,我不知道我应该用哪个名字来称呼它。感谢您的帮助!
  • @EntangledQuantum - 你没有对查询结果做任何事情,密码应该是结果的一部分,比如$user_query-&gt;password
  • 好的,我正在尝试查询,但它不起作用,我做错了什么。我到底要写什么?
  • @EntangledQuantum - 在第一步中,我将只专注于从数据库中存储和检索行。当你这样做时,可能是切换到 PDO 的好时机,这使得处理更容易和更安全(你的查询目前容易被 SQL 注入)。看看一个好的教程,也许是谷歌发现的:PDO Tutorial for MySQL Developers
猜你喜欢
  • 2014-09-12
  • 1970-01-01
  • 2011-03-09
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多