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 height="50">
    <td>New Passowrd :</td>
    <td><input type="password" name="npwd" id="npwd"></td>
    <tr height="50">
    <td>Confirm Password :</td>
    <td><input type="password" name="cpwd" id="cpwd"></td>
    <td><a href="index.php">Back to login Page</a></td>
    <td><input type="submit" name="Submit" value="Change Passowrd" /></td>

    Javascript Validation for change Password

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

    Script for Change Password

    $sql=mysqli_query($con,"SELECT password FROM userinfo where password='$oldpass' && email='$useremail'");
     $con=mysqli_query($con,"update userinfo set password=' $newpassword' where email='$useremail'");
    $_SESSION['msg1']="Password Changed Successfully !!";
    $_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

    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  :
    Password : Test@12345

    Recommended Tutorials for you


    Anuj kumar

    This is Anuj Kumar. I’m a professional web developer with 5+ years of experience. I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials. .


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

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

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Back to top button