How To Change Password Using PHP

Good Day!!!

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

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.         <fieldset>
  2.                 <legend>Change Password</legend>
  3.         <form method="post">
  4.                 <dl>
  5.                         <dt>
  6.                                 Old Password
  7.                         </dt>
  8.                                 <dd>
  9.                                         <input type="password" name="old_pass" placeholder="Old Password..." value="" required />
  10.                                 </dd>
  11.                 </dl>
  12.                 <dl>
  13.                         <dt>
  14.                                 New Password
  15.                         </dt>
  16.                                 <dd>
  17.                                         <input type="password" name="new_pass" placeholder="New Password..." value=""  required />
  18.                                 </dd>
  19.                 </dl>
  20.                 <dl>
  21.                         <dt>
  22.                                 Retype New Password
  23.                         </dt>
  24.                                 <dd>
  25.                                         <input type="password" name="re_pass" placeholder="Retype New Password..." value="" required />
  26.                                 </dd>
  27.                 </dl>
  28.                
  29.                 <p align="center">
  30.                         <input type="submit" class="btn" value="Reset Password" name="re_password" />
  31.                 </p>
  32.         </form>
  33.         </fieldset>
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.         <fieldset>
  52.                 <legend>Change Password</legend>
  53.         <?php
  54.                 $conn_db = mysql_connect("localhost","root","") or die();
  55.                 $sel_db = mysql_select_db("change_password",$conn_db) or die();
  56.                 if(isset($_POST['re_password']))
  57.                 {
  58.                 $old_pass=$_POST['old_pass'];
  59.                 $new_pass=$_POST['new_pass'];
  60.                 $re_pass=$_POST['re_pass'];
  61.                 $chg_pwd=mysql_query("select * from users where id='1'");
  62.                 $chg_pwd1=mysql_fetch_array($chg_pwd);
  63.                 $data_pwd=$chg_pwd1['password'];
  64.                 if($data_pwd==$old_pass){
  65.                 if($new_pass==$re_pass){
  66.                         $update_pwd=mysql_query("update users set password='$new_pass' where id='1'");
  67.                         echo "<script>alert('Update Sucessfully'); window.location='index.php'</script>";
  68.                 }
  69.                 else{
  70.                         echo "<script>alert('Your new and Retype Password is not match'); window.location='index.php'</script>";
  71.                 }
  72.                 }
  73.                 else
  74.                 {
  75.                 echo "<script>alert('Your old password is wrong'); window.location='index.php'</script>";
  76.                 }}
  77.         ?>
  78.        
  79.         <form method="post">
  80.                 <dl>
  81.                         <dt>
  82.                                 Old Password
  83.                         </dt>
  84.                                 <dd>
  85.                                         <input type="password" name="old_pass" placeholder="Old Password..." value="" required />
  86.                                 </dd>
  87.                 </dl>
  88.                 <dl>
  89.                         <dt>
  90.                                 New Password
  91.                         </dt>
  92.                                 <dd>
  93.                                         <input type="password" name="new_pass" placeholder="New Password..." value=""  required />
  94.                                 </dd>
  95.                 </dl>
  96.                 <dl>
  97.                         <dt>
  98.                                 Retype New Password
  99.                         </dt>
  100.                                 <dd>
  101.                                         <input type="password" name="re_pass" placeholder="Retype New Password..." value="" required />
  102.                                 </dd>
  103.                 </dl>
  104.                
  105.                 <p align="center">
  106.                         <input type="submit" class="btn" value="Reset Password" name="re_password" />
  107.                 </p>
  108.         </form>
  109.         </fieldset>
  110. </body>
  111. </html>
So what can you say about this work? Share your thoughts in the comment section below. Practice Coding. Thank you.

Comments

  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.         <fieldset>
  52.                 <legend>Change Password</legend>
  53.         <?php
  54.         session_start();
  55.    $getname=$_SESSION['user'];
  56.                 $conn_db = mysql_connect("localhost","root","") or die();
  57.                 $sel_db = mysql_select_db("diary",$conn_db) or die();
  58.                 if(isset($_POST['re_password']))
  59.                 {
  60.                 $old_pass=$_POST['old_pass'];
  61.                 $new_pass=$_POST['new_pass'];
  62.                 $re_pass=$_POST['re_pass'];
  63.                 $chg_pwd=mysql_query("select * from register where email='$getname'");
  64.                 $chg_pwd1=mysql_fetch_array($chg_pwd);
  65.                 $data_pwd=$chg_pwd1['password'];
  66.                 if($data_pwd==$old_pass){
  67.                 if($new_pass==$re_pass){
  68.                         $update_pwd=mysql_query("update register set password='$new_pass' where email='$getname'");
  69.                         echo "<script>alert('Update Sucessfully'); window.location='dailydiary.php'</script>";
  70.                 }
  71.                 else{
  72.                         echo "<script>alert('Your new and Retype Password is not match'); window.location='forgot.php'</script>";
  73.                 }
  74.                 }
  75.                 else
  76.                 {
  77.                 echo "<script>alert('Your old password is wrong'); window.location='forgot.php'</script>";
  78.                 }}
  79.         ?>
  80.  
  81.         <form method="post">
  82.                 <dl>
  83.                         <dt>
  84.                                 Old Password
  85.                         </dt>
  86.                                 <dd>
  87.                                         <input type="password" name="old_pass" placeholder="Old Password..." value="" required />
  88.                                 </dd>
  89.                 </dl>
  90.                 <dl>
  91.                         <dt>
  92.                                 New Password
  93.                         </dt>
  94.                                 <dd>
  95.                                         <input type="password" name="new_pass" placeholder="New Password..." value=""  required />
  96.                                 </dd>
  97.                 </dl>
  98.                 <dl>
  99.                         <dt>
  100.                                 Retype New Password
  101.                         </dt>
  102.                                 <dd>
  103.                                         <input type="password" name="re_pass" placeholder="Retype New Password..." value="" required />
  104.                                 </dd>
  105.                 </dl>
  106.  
  107.                 <p align="center">
  108.                         <input type="submit" class="btn" value="Reset Password" name="re_password" />
  109.                 </p>
  110.         </form>
  111.         </fieldset>
  112. </body>
  113. </html>

In reply to by Santu Chatterjee (not verified)

Thank you for posting it without numbers, obviously something the uploader cant do

This is not working mysqli.

Add new comment