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