How to change Password in php

In this tutorial I will explain how user can change their old password.First Create a html form with three fields.

  • Old Password
  • New Password
  • Confirm Password
  • Now Validate these fields with javascript. After this create a php script for change password . PHP script given below :
    HTML code for Change Password Form

    <p style="color:red;"><?php echo $_SESSION['msg1'];?><?php echo $_SESSION['msg1']="";?></p>
    <form name="chngpwd" action="" method="post" onSubmit="return valid();">
    <table align="center">
    <tr height="50">
    <td>Old Password :</td>
    <td><input type="password" name="opwd" id="opwd"></td>
    </tr>
    <tr height="50">
    <td>New Passowrd :</td>
    <td><input type="password" name="npwd" id="npwd"></td>
    </tr>
    <tr height="50">
    <td>Confirm Password :</td>
    <td><input type="password" name="cpwd" id="cpwd"></td>
    </tr>
    <tr>
    <td><a href="index.php">Back to login Page</a></td>
    <td><input type="submit" name="Submit" value="Change Passowrd" /></td>
    </tr>
     </table>
    </form>

    Javascript Validation for change Password

    <script >
    function valid()
    {
    if(document.chngpwd.opwd.value=="")
    {
    alert("Old Password Filed is Empty !!");
    document.chngpwd.opwd.focus();
    return false;
    }
    else if(document.chngpwd.npwd.value=="")
    {
    alert("New Password Filed is Empty !!");
    document.chngpwd.npwd.focus();
    return false;
    }
    else if(document.chngpwd.cpwd.value=="")
    {
    alert("Confirm Password Filed is Empty !!");
    document.chngpwd.cpwd.focus();
    return false;
    }
    else if(document.chngpwd.npwd.value!= document.chngpwd.cpwd.value)
    {
    alert("Password and Confirm Password Field do not match  !!");
    document.chngpwd.cpwd.focus();
    return false;
    }
    return true;
    }
    </script>

    Script for Change Password

    <?php
    session_start();
    include("dbconnection.php");
    if(isset($_POST['Submit']))
    {
     $oldpass=md5($_POST['opwd']);
     $useremail=$_SESSION['login'];
     $newpassword=md5($_POST['npwd']);
    $sql=mysqli_query($con,"SELECT password FROM userinfo where password='$oldpass' && email='$useremail'");
    $num=mysqli_fetch_array($sql);
    if($num>0)
    {
     $con=mysqli_query($con,"update userinfo set password=' $newpassword' where email='$useremail'");
    $_SESSION['msg1']="Password Changed Successfully !!";
    }
    else
    {
    $_SESSION['msg1']="Old Password not match !!";
    }
    }
    ?>

    Structure of userinfo Table

    CREATE TABLE `userinfo` (
      `id` int(11) NOT NULL,
      `email` varchar(255) NOT NULL,
      `password` varchar(250) NOT NULL,
      `updation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    Download Source code(How to change Password in php)
    Size: 209 KB
    Version: V 2.0

    How to run this script
    1. Download and Unzip file on your local system.
    2. Put this file inside root directory
    3. Database Configuration
    Database Configuration
    Open phpmyadmin
    Create Database test
    Import database userinfo.sql (available inside zip package)
    put this url in the browser http://localhost/changepassword
    Login details
    Username  : admin@test.com
    Password : Test@12345

    Leave A Reply

    Your email address will not be published.

    10 Comments
    1. Nur Anis Shahbani says

      it’s can’t download can u send in my email?
      anisshahbani34@gmail.com

      1. Anuj Kumar says

        Mail Sent.

    2. bhavin says

      thanks very much it’s working

    3. sutor rinaldi says

      how to change coding md5 to hashpassword
      im using $user_encrypted_password = password_hash($user_password, PASSWORD_DEFAULT);
      please help me

      1. Anuj Kumar says

        For password hashing visit this tutorial- https://phpgurukul.com/password-hashing-in-php/

    4. sutor rinaldi says

      how to change coding md5 to hashpassword
      im using $user_encrypted_password = password_hash($user_password, PASSWORD_DEFAULT);
      please help me

      1. Anuj Kumar says

        For password hashing visit this tutorial- https://phpgurukul.com/password-hashing-in-php/

    5. bhavin says

      thanks very much it’s working

    6. Nur Anis Shahbani says

      it’s can’t download can u send in my email?
      anisshahbani34@gmail.com

      1. Anuj Kumar says

        Mail Sent.

    This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

    Privacy & Cookies Policy