- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为我的网站创建我的更改密码网站,我的代码有一些问题......
出于某种原因,我很难在加密后在数据库中比较和替换密码。
我想要这个:
获取当前用户密码并将其与 $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>
最佳答案
有一个更简单的方法来解决这个问题:
// 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 中存储密码/散列不是很有用,如果你知道它是同一个用户,你知道他是否已经登录,只需在 session 中存储一个指示器,如 $_SESSION['is_logged_in'] 或只是用户名 $_SESSION['username'].
关于PHP crypt()、更新和比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28627803/
我正在将一些 C 代码从 Linux 迁移到 Mac OSX (yosemite)。 Mac OSX crypt() 函数(在 unistd.h 中,正如我所确定的)与 Linux 中的 gcc/gn
我确实对 crypt() PHP 函数感到困惑。 当第二个 crypt 显然使用不同的第二个参数时,以下两个 crypt 函数如何给出相同的输出?差异盐意味着差异哈希对吗? echo crypt("p
我正在为 CS50 做第 2 周的 pset。 使用 crypt 函数时,指向任何字符串密文的 char 指针总是指向我加密的最后一个东西。 例如: char password[] = "AAAA";
我已经使用 c 中的 crypt 函数来加密给定的字符串。我写了下面的代码, #include #include int main() { printf("%s\n",crypt("passw
我正在比较 PHP 的 crypt() 与 Python 的 crypt()。来自阅读 Python 手册: http://docs.python.org/2/library/crypt.html P
我正在比较 PHP 的 crypt() 与 Python 的 crypt()。来自阅读 Python 手册: http://docs.python.org/2/library/crypt.html P
出于某种原因,无论我尝试什么,使用crypt模块都会使用13字符哈希而不是sha-512。我见过无数关于问题的问题,但没有一个符合我的。难道就没有办法改变 crypt 方法吗? >>> import
我想用 crypt 散列密码使用 blowfish 加密的模块。 在 Fedora 29 上我得到了正确的结果: $ python3.7 Python 3.7.2 (default, Jan 3 2
我目前正在研究 Violent Python 一书中的一个示例。你可以看到我的实现 here 我现在正尝试在 Go 中实现相同的脚本来比较性能,注意我是 Go 的新手。打开文件并遍历这些行很好,但是我
背景 crypt 有两个定义,from the docs, 其中一个使用 unistd.h #define _XOPEN_SOURCE /* See feature_test_macros
我使用 dovecot 作为我的邮件传输代理,我的目标是使用 strongest password scheme我的系统支持:SHA512-CRYPT 或 SHA256-CRYPT(BLF-CRYPT
我正在学习 Python。我不明白为什么 hashlib.sha512(salt + password).hexdigest() 没有给出预期的结果。 我正在寻找与 Ulrich Drepper 的
我正在使用 GCC 4.6.0(在一个其他身份不明的平台上)。 我正在使用 crypt() 函数来加密密码。 我以前从未使用过该功能,所以我查看了主页: man 3 crypt 它说要包含 unist
我正在为 Linux 上的 MD5 哈希方案编写一个基本的密码破解程序 /etc/shadow file 。当我使用commons.codec时的DigestUtils或Crypt库,它们的哈希长度是
我更喜欢在 php 中使用 crypt 函数来进行密码加密和其他单向加密要求。因为我可以使用任何受支持的加密算法,通过更改 salt 并且几乎没有其他优势。通常,我不使用任何盐,它使用随机的 MD5
我有点困惑。当我用盐和密码调用 crypt 时,返回的字符串称为哈希或摘要?我很困惑,因为我知道从加密函数返回的内容称为哈希。但是当我们调用crypt 我们也传递 salt 作为参数。我们可以这样说:
本文整理了Java中freenet.crypt.Yarrow类的一些代码示例,展示了Yarrow类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些
我正在做一个练习,其中我需要将字符串中的字母替换为另一个字母(字母表中它前面的第四个字母)。我还必须消除除空格之外的所有非字母字符(“”)。到目前为止,我能够完成第一步(消除字符),但我无法替换字母。
我正在为我的网站创建我的更改密码网站,我的代码有一些问题...... 出于某种原因,我很难在加密后在数据库中比较和替换密码。 我想要这个: 获取当前用户密码并将其与 $oldpass 的输入值进行比较
当我使用 MD5 时,我曾经在数据库中创建一个 varchar(32) 列。但是,我开始使用 crypt(),据我所知,输出长度是可变的。那么我应该为 varchar 设置哪个长度? 最佳答案 返回的
我是一名优秀的程序员,十分优秀!