How To Change Password Using PHP

04th, January 2018 By

To change password in php you have to fire update query based on the user id you can do this easily by following these steps:

Step 1

You should have a form containing three text box old password,new password and confirm password these will be used for updating the password for example:

<div class="right_content_wrap">
 <h1>Change Password</h1>
 <form name="form1" action="" method="POST" >
 <table align="center" border="0"  width="50%">
 <tr><td><b>Old Password</b></td><td>:</td><td><input type="password" name="oldPassword" id="oldPassword" value="" /></td></tr>
 <tr><td><b>New Password</b></td><td>:</td><td><input type="password" name="newPassword" id="newPassword" value="" /></td></tr>
 <tr><td><b>Confirm Password</b></td><td>:</td><td><input type="password" name="confirmPassword" id="confirmPassword"value="" /></td></tr>
 <tr><td colspan="3" align="center"> <input type="submit" class="submit-button" name="submit" value="Update"/>

Here you can see the three text box and one submit button on click of the button it will pass value through the $_POST

Change Password

Step 2

Now you have to fetch the value from $_POST variable here you will have $_POST[‘oldPassword’],$_POST[‘newPassword’] and $_POST[‘confirmPassword’] if these will be empty then you have to make a condition if these are empty then it will not update the password in the database.

$oldPass = $_POST['oldPassword'];
$newPass = $_POST['newPassword'];
$confirmPass = $_POST['confirmPassword'];
if (trim($_POST['oldPassword']) == '') {
 $errorMsg[] = "Please enter Old password";
} else {
 if (trim($_POST['newPassword']) == "" || trim($_POST['confirmPassword']) == "") {
 $errorMsg[] = "New Password and Confirm Password can not empty";
 } else {
 if (strlen($_POST['newPassword']) < 5) {
 $errorMsg[] = 'New Password must be atleast 5 digits.';
 } elseif (trim($_POST['newPassword']) != trim($_POST['confirmPassword'])) {
 $errorMsg[] = "New Password and Confirm Password does not match";

Here we have checked if old password is not empty and new password and confirm password match .

Step 3

Now if the errormsg array is empty then we will fetch record from database based on the id and oldpassword .

if (count($errorMsg) > 0) {
 $_SESSION['ErrorMessage'] = $errorMsg;
} else {
 echo $results = mysql_query("select * from users where id='" . $id . "' and password='" . $_POST['oldPassword'] . "'");
 $count = mysql_num_rows($results);
 if ($count > 0) {
 $sql = "update users set password='" . $_POST['newPassword'] . "' where id='" . $id . "' and password='" . $_POST['oldPassword'] . "'";
 $resuls = mysql_query($sql);
 $msgs[] = "Update successfully !!";
 $_SESSION['SuccessMessage'] = $msgs;
 } else {
 $errorMsg[] = "Old password does not match";
 $_SESSION['ErrorMessage'] = $errorMsg;


if password query gives result then we will update the password based on the condition of id and oldpassword it will show the message that Password update successfully.

Leave a Reply

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