【发布时间】:2019-05-09 18:37:12
【问题描述】:
试图让用户可以更改密码,但由于密码已加密,用户必须先输入加密密码才能更改密码。
当用户点击“更改密码”时,他们会被发送到 change.php,然后再返回到 profile.php?error=wrong_pw
不习惯散列密码,因为到目前为止我只用 php 做过“简单”的网站,没有任何安全性。这也是为什么这很容易被黑客利用。
Change.php
<?php
session_start();
require 'dbc.php';
if (!isset($_POST['password']) || !isset($_POST['newpassword']) || !isset($_POST['renewpassword'])) {
header('Location: ../profile.php?error=wrong_info');
exit();
}
$user = $_SESSION['id'];
$password = $_POST['password'];
$newpassword = $_POST['newpassword'];
$renewpassword = $_POST['renewpassword'];
$sql = "SELECT * FROM users WHERE id='$user' AND pwd='$password'";
$result = mysqli_query($conn, $sql);
if($row = mysqli_fetch_assoc($result)){
if($newpassword == $renewpassword){
if($newpassword != ""){
$newsql = "UPDATE users SET pwd='$newpassword' WHERE id='$user'";
$newresult = mysqli_query($conn, $newsql);
header('Location: ../index.php');
}
else{
header('Location: ../profile.php?error=empty');
exit();
}
}
else{
header('Location: ../profile.php?error=match');
exit();
}
} else{
header('Location: ../profile.php?error=wrong_pw');
}
profile.php
<?php require "inc/header.inc.php"; ?>
<?php
if(isset($_GET['u'])){
$u = $_GET['u'];
}else{
$u = $_SESSION['id'];
}
$sql = "SELECT * FROM users WHERE id='$u'";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);
?>
<div class="container mt-3">
<table class="table">
<tr>
<th>Email</th>
<th>Username</th>
<?php
if ($u === $_SESSION['id']) {
echo "<th>Password</th>";
}
?>
</tr>
<tr>
<td><?php echo $user['email']; ?></td>
<td><?php echo $user['uid']; ?></td>
<?php
if ($u === $_SESSION['id']) {
echo '<th><a href="javascript:;" class="btn btn-danger" data-toggle="modal" data-target="#leModal">Change</a></th>';
}
?>
</tr>
</table>
</div>
<form action="change_theme.php" method="post">
<center><button class="btn btn-primary themebutton" type="submit" value="Change theme" action="change_theme.php">Change theme</button></center>
</form>
<div class="modal fade" id="leModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Change Password</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="article_q" action="engine/change.php" method="post">
<input type="hidden" name="user" value="<?php echo $_SESSION['id']; ?>">
<input type="password" name="password" placeholder="Current Password"><br><br>
<input type="password" name="newpassword" placeholder="New Password"><br><br>
<input type="password" name="renewpassword" placeholder="Re:Password"><br><br>
<button class="btn btn-danger" value="submit">Save</button>
</div>
<div class="modal-footer">
</form>
</div>
</div>
</div>
</div>
<?php require "inc/footer.inc.php"; ?>
【问题讨论】:
-
您的问题或问题究竟是什么?
-
对;什么没有按照您想要的方式工作?你得到什么结果?检查错误?你的问题不清楚(对我来说)。
-
您的要求尚不清楚。如果您还没有stackoverflow.com/help 以及其中的相关链接,那么对您有好处。请通读一遍,您将了解 Stack Overflow 是如何工作的,“学习技巧”。它会给你一个关于如何提出一个好问题的好主意,看看什么可以问,什么不应该问,以及对你的期望。这是为了帮助您在这里获得更好和积极的体验而制定和实施的,这是每个人都想要和追求的。
标签: php mysql sql session passwords