Errors 我编写了一个PHP程序,它使用用户新旧密码我的代码运行良好,但现在我的PHP程序中只需几行代码。 这是我编写的PHP代码,我想在其中添加几行代码,但是当我在其中编写新代码时,它会显示新的错误,我必须编写的代码向用户警告该用户&# 34;新密码应与旧密码不同"。当用户输入与旧密码网页相同的新密码的提交按钮时,此代码会警告用户。
这是我的PHP程序:
<?php
session_start();
// if ($_SESSION['user_name'] != "")
// {
// header("location:account.php");
// }
include('connection.php');
// header("Refresh: 20; URL=welcome.php");
// header("Refresh: 20; URL=http://www.stackoverflow.com/");
if(isset($_POST['submit']))
{
$old_password = $_POST['old_password'];
$new_password = $_POST['new_password'];
$query = $con->prepare("select password from tbl_users WHERE id = :user_id");
$query->bindParam(':user_id', $_SESSION['id']);
$query->setFetchMode(PDO::FETCH_ASSOC);
$query->execute();
$fetch = $query->fetch();
$old_pass = $fetch['password'];
if($old_password == $old_pass){
$stmt = $con->prepare("UPDATE tbl_users SET password = (:pass) WHERE id = :user_id");
$stmt->bindParam(':pass', $new_password, PDO::PARAM_STR);
$stmt->bindParam(':user_id', $_SESSION['id']);
// $stmt->execute();
$stmt->execute();
header("location:account.php");
}
else
{
echo "<script>alert('Wrong password! Enter your valid old password')</script>";
}
}
?>
HTML代码:
<!DOCTYPE html>
<html>
<head>
<title>project</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="registration.css">
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
</head>
<body>
<header><h1>Change Password</h1></header>
<form method="post" action="renew.php">
<br />
<input type="password" id="pwd2" placeholder="Enter your old password" name="old_password" required />
<br />
<input type="password" id="pwd1" placeholder="Enter your new password" name="new_password" required />
<center>
<!-- <div class="form-group"> -->
<div id="setPasswordMessage" style="display: none;"></div>
<!-- </div> -->
</center>
<br />
<div class="buttons">
<input type="submit" disabled="submit" class="btn" name="submit" value="Save">
</div>
<br />
</form>
<footer><h3>Copyright © vu.edu.pk (S1701F607E)</h3></footer>
<script type="text/javascript">
$(document).ready(function() {
var pwd1 = $('#pwd1'); //id of first password field
var pwd2 = $('#pwd2'); //id of second password field
var pwdIdSet = $('#setPasswordMessage'); //id of indicator element
setCheckPasswordStrength(pwd1,pwd2,pwdIdSet); //call password check function
});
function setCheckPasswordStrength(pwd1, pwd2, pwdIdSet)
{
/*=========== Start: Set Password Cretria Regular Expression ===================*/
//Password must contain 5 or more characters
var lowPassword = /(?=.{5,}).*/;
//Password must contain at least one digit and lower case letters .
var mediumPassword = /^(?=\S*?[a-z])(?=\S*?[0-9])\S{5,}$/;
//Password must contain at least one digit, one upper case letter and one lower case letter.
var averagePassword = /^(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S{5,}$/;
//Password must contain at least one digit, one upper case letter and one lower case letter.
var strongPassword = /^(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])(?=\S*?[^\w\*])\S{5,}$/;
/*=========== End: Set Password Cretria Regular Expression ===================*/
// test() method is used to test match in a string whether the value is matched in a string or not.
$(pwd1).on('keyup', function(e) {
var len = $('#pwd1').val().length;
document.getElementById("setPasswordMessage").style.display="block";
if(strongPassword.test(pwd1.val()))
{
pwdIdSet.removeClass().addClass('strongPassword').html("Strong! Please use this password!").css("display","block");
$(':input[type="submit"]').prop('disabled', false);
}
else if(averagePassword.test(pwd1.val()))
{
pwdIdSet.removeClass().addClass('averagePassword').html("Average! Tips: Enter special characters to make even stronger").css("display","block");
$(':input[type="submit"]').prop('disabled', true);
}
else if(mediumPassword.test(pwd1.val()))
{
pwdIdSet.removeClass().addClass('mediumPassword').html("Good! Tips: Enter uppercase letter to make strong").css("display","block");
$(':input[type="submit"]').prop('disabled', true);
}
else if(lowPassword.test(pwd1.val()))
{
pwdIdSet.removeClass().addClass('stilllowPassword').html("Still Weak! Tips: Enter digits to make good password").css("display","block");
$(':input[type="submit"]').prop('disabled', true);
}
else if(len < 1)
{
pwdIdSet.removeClass('lowPassword');
$('#setPasswordMessage').css("display","none");
$(':input[type="submit"]').prop('disabled', true);
}
else
{
pwdIdSet.removeClass().addClass('lowPassword').html("Very Weak! Please use 5 or more chars password)").css("display","block");
$(':input[type="submit"]').prop('disabled', true);
}
});
// $(pwd2).on('keyup', function(e) {
// if(pwd1.val() !== pwd2.val())
// {
// pwdIdSet.removeClass().addClass('lowPassword').html("Passwords do not match!");
// }else{
// pwdIdSet.removeClass().addClass('goodpass').html("Passwords match!");
// }
// });
}
</script>
</body>
</html>
我必须在PHP代码中添加此代码,但在哪个地方以及如何使用。
if($old_password == $new_password)
{
echo "<script>alert('New password should be different with old password')</script>";
}
答案 0 :(得分:0)
在您进行查询之前立即检查。
<img>
答案 1 :(得分:0)
如果在任何输出后发送标头,则PHP抛出错误。因此,如果在// header("location:account.php");
echo "<script>document.location.href = 'account.php';</script>";
之前有任何输出,则会发生错误。尝试通过JS设置新位置:
if($old_password == $old_pass){
if($old_password == $new_password)
{
echo "<script>alert('New password should be different with old password')</script>";
} else {
$stmt = $con->prepare("UPDATE tbl_users SET password = (:pass) WHERE id = :user_id");
$stmt->bindParam(':pass', $new_password, PDO::PARAM_STR);
$stmt->bindParam(':user_id', $_SESSION['id']);
// $stmt->execute();
$stmt->execute();
// header("location:account.php");
echo "<script>document.location.href = 'account.php';</script>";
}
}
else....
完整的代码看起来像这样:
undefined
答案 2 :(得分:0)
在设置变量$old_password
和$new_password
之后以及执行任何数据库查询之前,您应该进行此检查(如果旧密码和新密码相同,则不希望执行这些检查):
if(isset($_POST['submit']))
{
$old_password = $_POST['old_password'];
$new_password = $_POST['new_password'];
if($old_password == $new_password)
{
echo "<script>alert('New password should be different with old password')</script>";
}
else
{
$query = $con->prepare("select password from tbl_users WHERE id = :user_id");
$query->bindParam(':user_id', $_SESSION['id']);
$query->setFetchMode(PDO::FETCH_ASSOC);
$query->execute();
$fetch = $query->fetch();
$old_pass = $fetch['password'];
if($old_password == $old_pass){
$stmt = $con->prepare("UPDATE tbl_users SET password = (:pass) WHERE id = :user_id");
$stmt->bindParam(':pass', $new_password, PDO::PARAM_STR);
$stmt->bindParam(':user_id', $_SESSION['id']);
// $stmt->execute();
$stmt->execute();
header("location:account.php");
}
else
{
echo "<script>alert('Wrong password! Enter your valid old password')</script>";
}
}
}
虽然它与编码无关,但我只想注意你的错误信息是不正确的英文。某些东西与其他东西“不同”,而不是“与......不同”。使用此:
您的新密码必须与旧密码不同