PHP

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 type="text/javascript">
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;
View Demo
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

Recommended Tutorials for you


Related posts

PHP-Basic Development Concepts

Anuj Kumar

How to Fetch data from database Using OOPS In PHP

Anuj Kumar

Strings in PHP

Anuj Kumar

PHP Constants

Anuj Kumar

Get City Country By IP Address in PHP

Anuj Kumar

Conditional Statement

Anuj Kumar

PHP – Variables

Anuj Kumar

How to Salt & Hash a Password using Sha256

Anuj Kumar

Date And Time Formatting With PHP

Anuj Kumar

10 comments

Nur Anis Shahbani November 10, 2018 at 5:58 am

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

Reply
Anuj Kumar November 10, 2018 at 6:18 am

Mail Sent.

Reply
bhavin November 10, 2018 at 5:58 am

thanks very much it’s working

Reply
sutor rinaldi November 10, 2018 at 5:58 am

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

Reply
Anuj Kumar November 10, 2018 at 6:18 am

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

Reply
sutor rinaldi November 3, 2018 at 1:25 pm

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

Reply
Anuj Kumar November 3, 2018 at 6:38 pm

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

Reply
bhavin October 20, 2018 at 12:39 pm

thanks very much it’s working

Reply
Nur Anis Shahbani October 10, 2017 at 2:48 am

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

Reply
Anuj Kumar October 10, 2017 at 11:48 am

Mail Sent.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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