Change Password Using PHP

Change Password Using PHP

In this tutorial, we are going to learn on Change Password Using PHP. In any Web Application, changing password is an important feature. Now, let us create a change password using PHP script. database name - sourcecodester old password : sourcecodester

Directions:

For CSS Codes
  1. <style type="text/css">
  2. fieldset {
  3. width:500px;
  4. border:5px dashed #CCCCCC;
  5. margin:0 auto;
  6. border-radius:5px;
  7. }
  8.  
  9. legend {
  10. color: blue;
  11. font-size: 25px;
  12. }
  13.  
  14. dl {
  15. float: right;
  16. width: 390px;
  17. }
  18.  
  19. dt {
  20. width: 180px;
  21. color: brown;
  22. font-size: 19px;
  23. }
  24.  
  25. dd {
  26. width:200px;
  27. float:left;
  28. }
  29.  
  30. dd input {
  31. width: 200px;
  32. border: 2px dashed #DDD;
  33. font-size: 15px;
  34. text-indent: 5px;
  35. height: 28px;
  36. }
  37.  
  38. .btn {
  39. color: #fff;
  40. background-color: dimgrey;
  41. height: 38px;
  42. border: 2px solid #CCC;
  43. border-radius: 10px;
  44. float: right;
  45. }
  46.  
  47. </style>

For HTML Codes

  1. <legend>Change Password</legend>
  2. <form method="post">
  3. <dl>
  4. <dt>
  5. Old Password
  6. </dt>
  7. <dd>
  8. <input type="password" name="old_pass" placeholder="Old Password..." value="" required />
  9. </dd>
  10. </dl>
  11. <dl>
  12. <dt>
  13. New Password
  14. </dt>
  15. <dd>
  16. <input type="password" name="new_pass" placeholder="New Password..." value="" required />
  17. </dd>
  18. </dl>
  19. <dl>
  20. <dt>
  21. Retype New Password
  22. </dt>
  23. <dd>
  24. <input type="password" name="re_pass" placeholder="Retype New Password..." value="" required />
  25. </dd>
  26. </dl>
  27.  
  28. <p align="center">
  29. <input type="submit" class="btn" value="Reset Password" name="re_password" />
  30. </p>
  31. </form>

For PHP Codes

  1. <?php
  2. $conn_db = mysql_connect("localhost","root","") or die();
  3. $sel_db = mysql_select_db("change_password",$conn_db) or die();
  4. if(isset($_POST['re_password']))
  5. {
  6. $old_pass=$_POST['old_pass'];
  7. $new_pass=$_POST['new_pass'];
  8. $re_pass=$_POST['re_pass'];
  9. $chg_pwd=mysql_query("select * from users where id='1'");
  10. $chg_pwd1=mysql_fetch_array($chg_pwd);
  11. $data_pwd=$chg_pwd1['password'];
  12. if($data_pwd==$old_pass){
  13. if($new_pass==$re_pass){
  14. $update_pwd=mysql_query("update users set password='$new_pass' where id='1'");
  15. echo "<script>alert('Update Sucessfully'); window.location='index.php'</script>";
  16. }
  17. else{
  18. echo "<script>alert('Your new and Retype Password is not match'); window.location='index.php'</script>";
  19. }
  20. }
  21. else
  22. {
  23. echo "<script>alert('Your old password is wrong'); window.location='index.php'</script>";
  24. }}
  25. ?>
Full Source Code
  1. <!DOCTYPE html>
  2. <title>Change Password</title>
  3. <style type="text/css">
  4. fieldset {
  5. width:500px;
  6. border:5px dashed #CCCCCC;
  7. margin:0 auto;
  8. border-radius:5px;
  9. }
  10.  
  11. legend {
  12. color: blue;
  13. font-size: 25px;
  14. }
  15.  
  16. dl {
  17. float: right;
  18. width: 390px;
  19. }
  20.  
  21. dt {
  22. width: 180px;
  23. color: brown;
  24. font-size: 19px;
  25. }
  26.  
  27. dd {
  28. width:200px;
  29. float:left;
  30. }
  31.  
  32. dd input {
  33. width: 200px;
  34. border: 2px dashed #DDD;
  35. font-size: 15px;
  36. text-indent: 5px;
  37. height: 28px;
  38. }
  39.  
  40. .btn {
  41. color: #fff;
  42. background-color: dimgrey;
  43. height: 38px;
  44. border: 2px solid #CCC;
  45. border-radius: 10px;
  46. float: right;
  47. }
  48.  
  49. </head>
  50.  
  51. <legend>Change Password</legend>
  52. <?php
  53. $conn_db = mysql_connect("localhost","root","") or die();
  54. $sel_db = mysql_select_db("change_password",$conn_db) or die();
  55. if(isset($_POST['re_password']))
  56. {
  57. $old_pass=$_POST['old_pass'];
  58. $new_pass=$_POST['new_pass'];
  59. $re_pass=$_POST['re_pass'];
  60. $chg_pwd=mysql_query("select * from users where id='1'");
  61. $chg_pwd1=mysql_fetch_array($chg_pwd);
  62. $data_pwd=$chg_pwd1['password'];
  63. if($data_pwd==$old_pass){
  64. if($new_pass==$re_pass){
  65. $update_pwd=mysql_query("update users set password='$new_pass' where id='1'");
  66. echo "<script>alert('Update Sucessfully'); window.location='index.php'</script>";
  67. }
  68. else{
  69. echo "<script>alert('Your new and Retype Password is not match'); window.location='index.php'</script>";
  70. }
  71. }
  72. else
  73. {
  74. echo "<script>alert('Your old password is wrong'); window.location='index.php'</script>";
  75. }}
  76. ?>
  77.  
  78. <form method="post">
  79. <dl>
  80. <dt>
  81. Old Password
  82. </dt>
  83. <dd>
  84. <input type="password" name="old_pass" placeholder="Old Password..." value="" required />
  85. </dd>
  86. </dl>
  87. <dl>
  88. <dt>
  89. New Password
  90. </dt>
  91. <dd>
  92. <input type="password" name="new_pass" placeholder="New Password..." value="" required />
  93. </dd>
  94. </dl>
  95. <dl>
  96. <dt>
  97. Retype New Password
  98. </dt>
  99. <dd>
  100. <input type="password" name="re_pass" placeholder="Retype New Password..." value="" required />
  101. </dd>
  102. </dl>
  103.  
  104. <p align="center">
  105. <input type="submit" class="btn" value="Reset Password" name="re_password" />
  106. </p>
  107. </form>
  108. </body>
  109. </html>
  Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment