How to Change Password using PHP

Getting Started

I've used Bootstrap in this tutorial which is included in the downloadable but if you want, you can download it yourself using this link. Take not that I'm using password_hash and password verify function which is available on PHP >=5.

Creating our Database

First we create our database which contains our sample user. I've included a SQL file in the downloadable of this tutorial. All you have to do is import the said file. If you have no idea on how to import, please visit my tutorial How import .sql file to restore MySQL database. You should be able to create a database named dbase.

Creating our Login Form

Next, we create our sample login form. Create a new file, name it as index.php and paste the codes below.
  1. <?php
  2.  
  3. //redirect to home if session has been set
  4. if(isset($_SESSION['user'])){
  5. header('location:home.php');
  6. exit();
  7. }
  8. ?>
  9. <!DOCTYPE html>
  10. <html>
  11. <head>
  12. <meta charset="utf-8">
  13. <title>How to Change Password using PHP</title>
  14. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  15. </head>
  16. <body>
  17. <div class="container">
  18. <h1 class="page-header text-center">Change Password using PHP</h1>
  19. <div class="row">
  20. <div class="col-sm-4 col-sm-offset-4 panel panel-default" style="padding:20px;">
  21. <form method="POST" action="login.php">
  22. <p class="text-center" style="font-size:30px;"><b>Login</b></p>
  23. <hr>
  24. <div class="form-group">
  25. <label for="username">Username:</label>
  26. <input type="text" name="username" id="username" class="form-control">
  27. </div>
  28. <div class="form-group">
  29. <label for="password">Password:</label>
  30. <input type="password" name="password" id="password" class="form-control">
  31. </div>
  32. <button type="submit" name="login" class="btn btn-primary"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  33. </form>
  34. <?php
  35. if(isset($_SESSION['error'])){
  36. ?>
  37. <div class="alert alert-danger text-center" style="margin-top:20px;">
  38. <?php echo $_SESSION['error']; ?>
  39. </div>
  40. <?php
  41.  
  42. unset($_SESSION['error']);
  43. }
  44. ?>
  45. </div>
  46. </div>
  47. </div>
  48. </body>
  49. </html>

Creating our Login Script

Lastly, we create our login script that checks our user. Create a new file, name it as login.php and paste the codes below.
  1. <?php
  2.  
  3. if(isset($_POST['login'])){
  4. //connection
  5. $conn = new mysqli('localhost', 'root', '', 'dbase');
  6.  
  7. //get the user with the username
  8. $sql = "SELECT * FROM users WHERE username = '".$_POST['username']."'";
  9. $query = $conn->query($sql);
  10. if($query->num_rows > 0){
  11. $row = $query->fetch_assoc();
  12. //verify password
  13. if(password_verify($_POST['password'], $row['password'])){
  14. $_SESSION['user'] = $row['id'];
  15. }
  16. else{
  17. $_SESSION['error'] = 'Password incorrect';
  18. }
  19. }
  20. else{
  21. $_SESSION['error'] = 'No account with that username';
  22. }
  23.  
  24. }
  25. else{
  26. $_SESSION['error'] = 'Fill up login form first';
  27. }
  28.  
  29. header('location: index.php');
  30.  
  31. ?>

Creating our Homepage

Next, we create the page where our verified users are directed. It also contains our change password form. Create a new file, name it as home.php and paste the codes below.
  1. <?php
  2.  
  3. //redirect ot login page if not logged in
  4. if(!isset($_SESSION['user'])){
  5. header('location:index.php');
  6. exit();
  7. }
  8.  
  9. //connection
  10. $conn = new mysqli('localhost', 'root', '', 'dbase');
  11.  
  12. //get user details
  13. $sql = "SELECT * FROM users WHERE id = '".$_SESSION['user']."'";
  14. $query = $conn->query($sql);
  15. $row = $query->fetch_assoc();
  16.  
  17. ?>
  18. <!DOCTYPE html>
  19. <html>
  20. <head>
  21. <meta charset="utf-8">
  22. <title>How to Change Password using PHP</title>
  23. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  24. </head>
  25. <body>
  26. <div class="container">
  27. <h1 class="page-header text-center">Change Password using PHP</h1>
  28. <div class="row">
  29. <div class="col-sm-4 col-sm-offset-4 panel panel-default" style="padding:20px;">
  30. <h3>Welcome, <?php echo $row['username']; ?>
  31. <span class="pull-right">
  32. <a href="logout.php" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-log-out"></span> Logout</a>
  33. </span>
  34. </h3>
  35. <hr>
  36. <form method="POST" action="change_password.php">
  37. <div class="form-group">
  38. <label for="old">Old Password:</label>
  39. <input type="password" name="old" id="old" class="form-control" value="<?php echo (isset($_SESSION['old'])) ? $_SESSION['old'] : ''; ?>">
  40. </div>
  41. <div class="form-group">
  42. <label for="new">New Password:</label>
  43. <input type="password" name="new" id="new" class="form-control" value="<?php echo (isset($_SESSION['new'])) ? $_SESSION['new'] : ''; ?>">
  44. </div>
  45. <div class="form-group">
  46. <label for="retype">Retype New Password:</label>
  47. <input type="password" name="retype" id="retype" class="form-control" value="<?php echo (isset($_SESSION['retype'])) ? $_SESSION['retype'] : ''; ?>">
  48. </div>
  49. <button type="submit" name="update" class="btn btn-success"><span class="glyphicon glyphicon-check"></span> Update</button>
  50. </form>
  51. <?php
  52. if(isset($_SESSION['error'])){
  53. ?>
  54. <div class="alert alert-danger text-center" style="margin-top:20px;">
  55. <?php echo $_SESSION['error']; ?>
  56. </div>
  57. <?php
  58.  
  59. unset($_SESSION['error']);
  60. }
  61. if(isset($_SESSION['success'])){
  62. ?>
  63. <div class="alert alert-success text-center" style="margin-top:20px;">
  64. <?php echo $_SESSION['success']; ?>
  65. </div>
  66. <?php
  67.  
  68. unset($_SESSION['success']);
  69. }
  70. ?>
  71. </div>
  72. </div>
  73. </div>
  74. </body>
  75. </html>

Creating our Change Password Script

Next, we create the script that changes the users password. Create a new file, name it as change_password.php.
  1. <?php
  2.  
  3. if(isset($_POST['update'])){
  4. //get POST data
  5. $old = $_POST['old'];
  6. $new = $_POST['new'];
  7. $retype = $_POST['retype'];
  8.  
  9. //create a session for the data incase error occurs
  10. $_SESSION['old'] = $old;
  11. $_SESSION['new'] = $new;
  12. $_SESSION['retype'] = $retype;
  13.  
  14. //connection
  15. $conn = new mysqli('localhost', 'root', '', 'dbase');
  16.  
  17. //get user details
  18. $sql = "SELECT * FROM users WHERE id = '".$_SESSION['user']."'";
  19. $query = $conn->query($sql);
  20. $row = $query->fetch_assoc();
  21.  
  22. //check if old password is correct
  23. if(password_verify($old, $row['password'])){
  24. //check if new password match retype
  25. if($new == $retype){
  26. //hash our password
  27. $password = password_hash($new, PASSWORD_DEFAULT);
  28.  
  29. //update the new password
  30. $sql = "UPDATE users SET password = '$password' WHERE id = '".$_SESSION['user']."'";
  31. if($conn->query($sql)){
  32. $_SESSION['success'] = "Password updated successfully";
  33. //unset our session since no error occured
  34. unset($_SESSION['old']);
  35. unset($_SESSION['new']);
  36. unset($_SESSION['retype']);
  37. }
  38. else{
  39. $_SESSION['error'] = $conn->error;
  40. }
  41. }
  42. else{
  43. $_SESSION['error'] = "New and retype password did not match";
  44. }
  45. }
  46. else{
  47. $_SESSION['error'] = "Incorrect Old Password";
  48. }
  49. }
  50. else{
  51. $_SESSION['error'] = "Input needed data to update first";
  52. }
  53.  
  54. header('location: home.php');
  55.  
  56. ?>

Creating our Logout Script

Lastly, we create our logout script. Create a new file, name it as logout.php and paste the codes below.
  1. <?php
  2. header('location: index.php');
  3. ?>
That ends this tutorial. Happy Coding :)

Comments

Submitted bymcfion Tue, 04/27/2021 - 00:14

Thanks a lot, your code really helped me. Thank you

Submitted byasdasqwe (not verified)on Sat, 01/07/2023 - 06:33

Creating our Database First we create our database which contains our sample user. I've included a SQL file in the downloadable of this tutorial. All you have to do is import the said file. If you have no idea on how to import, please visit my tutorial How import .sql file to restore MySQL database. You should be able to create a database named dbase.
Submitted bywhat is the pa… (not verified)on Wed, 05/31/2023 - 11:56

what is the password to test change password ??? username : nurhodelta pass: $2y$10$AP027M5jhULJPIBAUiCa0e0phP1UAQBlKqTLLQZ2.UL44x5DdUwHq

Add new comment