I want to change the password by verifying/validating first it with the old password first. I don't know how to validate the old password because I'm not good at PHP. Please help me thank you in advance! Below are the code.
HTML:
<form name="frm" class="post-form-wrapper" action="app/new-pass.php" method="POST">
<div class="row gap-20">
<?php include 'constants/check_reply.php'; ?>
<?php
require '../constants/db_config.php';
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT login FROM tbl_users WHERE member_no='$myid'");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row)
{
$oldpass = $row['login'];
}
}catch(PDOException $e)
{
}
?>
<div class="col-sm-6 col-md-4">
<!-- <div class="form-group">
<label>Old Password <?php echo "$oldpass"; ?> </label>
<input type="password" class="form-control" name="oldpassword" required placeholder="Enter your old password">
</div> -->
<div class="form-group">
<label>New Password</label>
<input type="password" class="form-control" name="password" required
placeholder="Enter your new password">
</div>
</div>
<div class="clear"></div>
<div class="col-sm-6 col-md-4">
<div class="form-group">
<label>Confirm Password</label>
<input type="password" class="form-control" name="confirmpassword" required
placeholder="Confirm your new password">
</div>
</div>
<div class="col-sm-12 mt-10">
<button type="submit" onclick="return check_passwords();" class="btn btn-primary">Update</button>
<button type="reset" class="btn btn-primary btn-inverse">Cancel</a>
</div>
</div>
</form>
I think it would be validated using JavaScript but I'm not really sure. JavaScript:
function check_passwords() {
// if(md5(frm.oldpassword.value) != $oldpass )
// {
// alert("Invalid Old Password");
// frm.oldpassword.focus();
// return false;
// }
if (frm.password.value == "") {
alert("Enter the Password.");
frm.password.focus();
return false;
}
if ((frm.password.value).length < 8) {
alert("Password should be minimum 8 characters.");
frm.password.focus();
return false;
}
if (frm.confirmpassword.value == "") {
alert("Enter the Confirmation Password.");
return false;
}
if (frm.confirmpassword.value != frm.password.value) {
alert("Password confirmation does not match.");
return false;
}
return true;
} <
This is the PHP code for changing the password. PHP:
$new_password = md5($_POST['password']);
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("UPDATE tbl_users SET login = :newpassword WHERE member_no='$myid'");
$stmt->bindParam(':newpassword', $new_password);
$stmt->execute();
header("location:../change-password.php?r=9564");
}catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}