What's new

Help How to create change password

Status
Not open for further replies.

Alieson09

Forum Expert
Joined
Aug 30, 2021
Posts
2,246
Solutions
27
Reaction
2,961
Points
2,085
how to create change password in this code
PHP:
<?php
session_start();
include('includes/connect.php');

if (!isset($_SESSION['bpmsaid']) || strlen($_SESSION['bpmsaid']) == 0) {
 
} else {
  if(isset($_POST['submit'])) {
    $adminid=$_SESSION['bpmsaid'];
    $cpassword=($_POST['currentpassword']);
    $newpassword=($_POST['newpassword']);
    $query=mysqli_query($con,"select ID from login where ID='$adminid' and password='$cpassword'");
    $row=mysqli_fetch_array($query);
    if($row>0){
      $ret=mysqli_query($con,"update login set password='$newpassword' where ID='$adminid'");
      echo '<script>alert("Your password successully changed.")</script>';
    }
    else {
      echo '<script>alert("Your current password is wrong.")</script>';
    }
  }
}
?>
<script type="text/javascript">
function checkpass()
{
if(document.changepassword.newpassword.value!=document.changepassword.confirmpassword.value)
{
alert('New Password and Confirm Password field does not match');
document.changepassword.confirmpassword.focus();
return false;
}
return true;
}
</script>
<body>
   <?php include_once('includes/header.php');?>
   <?php include_once('includes/navbar.php');?>

        <div class="page-title">
                                
                                    <li><a href="dashboard.php">Dashboard</a></li>
                                    <li><a href="change-password.php">Change Password</a></li>
                                    <li class="active">Change Password</li>
                                
                            </div>
        <div class="content">
            <div class="animated fadeIn">
                <div class="row">
                    <div class="col-lg-6">
                        <div class="card">                         
                        </div> <!-- .card -->
                    </div><!--/.col-->
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-header">
                                <strong>Change </strong> Password
                            </div>
                            <div class="card-body card-block">
                                <form action="" method="post" enctype="multipart/form-data" class="form-horizontal" name="changepassword" onsubmit="return checkpass();">                         
                                   <?php
$adminid=$_SESSION['bpmsaid'];
$ret=mysqli_query($con,"select * from login where ID='$adminid'");
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {
?>
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="text-input" class=" form-control-label">Current Password</label></div>
                                        <div class="col-12 col-md-9"><input type="password" name="currentpassword" class=" form-control" required= "true" value=""></div>
                                    </div>
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="email-input" class=" form-control-label">New Password</label></div>
                                        <div class="col-12 col-md-9"><input type="password" name="newpassword" class="form-control" value="" required="true"></div>
                                    </div>
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="password-input" class=" form-control-label">Confirm Password</label></div>
                                        <div class="col-12 col-md-9"> <input type="password" name="confirmpassword" class="form-control" value="" required="true"></div>
                                    </div>                   
                                    <?php } ?>
                                   <p style="text-align: center;"> <button type="submit" class="btn btn-primary btn-sm" name="submit" >Change</button></p>
                                </form>
                            </div>                           
                        </div>                       
                    </div>
                    <div class="col-lg-6">                 
                </div>
            </div>
        </div><!-- .animated -->
    </div><!-- .content -->

    <div class="clearfix"></div>
    <?php
include('includes/scripts.php');
include('includes/footer.php');
?>
 
The code you have provided is already a working "change password" functionality that allows the user to change their password. The logic of the code is as follows:

1. A session is started and the user is redirected to the login page if they are not logged in.

2. If the user is logged in and submits the change password form:

a. The current password entered in the form is compared with the user's existing password in the database.

b. If the current password is correct, the user's password in the database is updated with the new password.

3. The HTML code renders the form where the user can enter their current password, new password, and confirm password.

To make any changes to this code, you can modify the HTML code or the PHP code as per your requirements.
 
Status
Not open for further replies.

Similar threads

Back
Top