PHP - Simple Change Password Confirmation

Submitted by: 
Visitors have accessed this post 990 times.

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.