Skip to main content

PHP - Simple Change Password Confirmation


In this tutorial we will create a Simple Change Password Confirmation Using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_password, after that click Import then locate the database file inside the folder of the application then click ok.
tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_password') or die(mysqli_error());
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as shown below.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Change Password Confirmation</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add user</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Username</th>
  23. <th>Password</th>
  24. <th>Firstname</th>
  25. <th>Lastname</th>
  26. <th>Action</th>
  27. </tr>
  28. </thead>
  29. <tbody style="background-color:#fff;">
  30. <?php
  31. require 'conn.php';
  32. $query = $conn->query("SELECT * FROM `user`") or die($conn->error());
  33. while($fetch = $query->fetch_array()){
  34. ?>
  35. <tr>
  36. <td><?php echo $fetch['username']?></td>
  37. <td><?php echo str_replace($fetch['password'], "********", $fetch['password'])?></td>
  38. <td><?php echo $fetch['firstname']?></td>
  39. <td><?php echo $fetch['lastname']?></td>
  40. <td><button type="button" class="btn btn-warning" data-toggle="modal" data-target="#update_modal<?php echo $fetch['user_id']?>">Change Password</button></td>
  41. </tr>
  42.  
  43. <div class="modal fade" id="update_modal<?php echo $fetch['user_id']?>" tabindex="-1" role="dialog" aria-hidden="true">
  44. <div class="modal-dialog" role="document">
  45. <form action="update_password.php" method="POST" enctype="multipart/form-data">
  46. <div class="modal-content">
  47. <div class="modal-body">
  48. <div class="col-md-2"></div>
  49. <div class="col-md-8">
  50. <div class="form-group">
  51. <label>Username</label>
  52. <input type="hidden" name="user_id" value="<?php echo $fetch['user_id']?>"/>
  53. <input class="form-control" type="text" name="username" value="<?php echo $fetch['username']?>" required="required" />
  54. </div>
  55. <div class="form-group">
  56. <label>New Password</label>
  57. <input class="form-control" type="password" maxlength="12" name="newpassword" required="required"/>
  58. </div>
  59. <div class="form-group">
  60. <label>Confirm Password</label>
  61. <input class="form-control" type="password" maxlength="12" name="confirmpassword" required="required"/>
  62. </div>
  63. <div class="form-group">
  64. <label>Old Password</label>
  65. <input class="form-control" type="password" maxlength="12" name="oldpassword" required="required"/>
  66. </div>
  67. <div class="form-group">
  68. <label>Firstname</label>
  69. <input class="form-control" type="text" name="firstname" value="<?php echo $fetch['firstname']?>" required="required"/>
  70. </div>
  71. <div class="form-group">
  72. <label>Lastname</label>
  73. <input class="form-control" type="text" name="lastname" value="<?php echo $fetch['lastname']?>" required="required"/>
  74. </div>
  75. </div>
  76. </div>
  77. <div style="clear:both;"></div>
  78. <div class="modal-footer">
  79. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  80. <button name="update" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Update</button>
  81. </div>
  82. </div>
  83. </form>
  84. </div>
  85. </div>
  86.  
  87.  
  88.  
  89. <?php
  90. }
  91. ?>
  92. </tbody>
  93. </table>
  94. </div>
  95. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  96. <div class="modal-dialog" role="document">
  97. <form action="save_user.php" method="POST" enctype="multipart/form-data">
  98. <div class="modal-content">
  99. <div class="modal-body">
  100. <div class="col-md-2"></div>
  101. <div class="col-md-8">
  102. <div class="form-group">
  103. <label>Username</label>
  104. <input class="form-control" type="text" name="username" required="required" />
  105. </div>
  106. <div class="form-group">
  107. <label>Password</label>
  108. <input class="form-control" type="password" maxlength="12" name="password" required="required"/>
  109. </div>
  110. <div class="form-group">
  111. <label>Firstname</label>
  112. <input class="form-control" type="text" name="firstname" required="required"/>
  113. </div>
  114. <div class="form-group">
  115. <label>Lastname</label>
  116. <input class="form-control" type="text" name="lastname" required="required"/>
  117. </div>
  118. </div>
  119. </div>
  120. <div style="clear:both;"></div>
  121. <div class="modal-footer">
  122. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  123. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  124. </div>
  125. </div>
  126. </form>
  127. </div>
  128. </div>
  129. </body>
  130. <script src="js/jquery-3.2.1.min.js"></script>
  131. <script src="js/bootstrap.js"></script>
  132. </html>

Creating PHP Query

This code contains the php query of the application. This code section is consist of two functionalities the saving of user, and updating user. The first function will save the data inputs to the database server while the second will update the user account, but for password it need to fill up the requirement to be able to update an account To do that just copy and write this block of codes inside the text editor, then save it as shown below.
save_user.php

  1. <?php
  2. require_once 'conn.php';
  3. if(ISSET($_POST['save'])){
  4. $username = $_POST['username'];
  5. $password = $_POST['password'];
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8.  
  9. $conn->query("INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$username', '$password')") or die($conn->error());
  10.  
  11. header('location: index.php');
  12. }
  13. ?>

update_password

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['update'])){
  5. $user_id = $_POST['user_id'];
  6. $username = $_POST['username'];
  7. $newpassword = $_POST['newpassword'];
  8. $confirmpassword = $_POST['confirmpassword'];
  9. $oldpassword = $_POST['oldpassword'];
  10. $firstname = $_POST['firstname'];
  11. $lastname = $_POST['lastname'];
  12.  
  13. if($newpassword == $confirmpassword){
  14. $query = $conn->query("SELECT * FROM `user` WHERE `user_id` = '$user_id' && `password` = '$oldpassword'") or die($conn->error());
  15. $valid = $query->num_rows;
  16.  
  17. if($valid > 0){
  18. $conn->query("UPDATE `user` SET `firstname` = '$firstname', `lastname` = '$lastname', `username` = '$username', `password` = '$newpassword' WHERE `user_id` = '$user_id'") or die($conn->error());
  19. echo "<script>alert('Password updated')</script>";
  20. echo "<script>window.location = 'index.php'</script>";
  21. }else{
  22. echo "<script>alert('Password does not match to old password')</script>";
  23. echo "<script>window.location = 'index.php'</script>";
  24. }
  25.  
  26. }else{
  27. echo "<script>alert('Password does not match')</script>";
  28. echo "<script>window.location = 'index.php'</script>";
  29. }
  30.  
  31.  
  32.  
  33.  
  34.  
  35. }
  36. ?>

There you have it we successfully created Simple Change Password Confirmation using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.