PHP Change Password script using Mysqli

PHP Change Password script using Mysqli

The Change password feature option(in a setting) in a web application is to let the all user change or admin change their old password(at a security) at some periodic interval phase.ng4free.com In this Post Learn to PHP Change Password script using Mysqli

PHP Change Password Script using mysqli. After the change password successful your form submits, the PHP server side code will access Mysqli database to get or fetch current password from the Database retrived. If this database value is both value matched with the your forms current change password value, then the password will be successfully changed.

Core Logic

  • First step, you have created simple HTML form with respective all the text-filed(old password, new password, Retype password).
  • Check the basic validation whether the new password and Retype password is same or not and all empty fields validation.
  • After the check javascript validation,and then check serverside you have to check old password (true or false) valid or not. The password is the same or match then allow skip to next level.
  • Simple old password type and new password type is same(Both are same) don’t allow the any option change the password.

PHP Change Password Script

query("SELECT *from users WHERE id='" . $_SESSION["userId"] . "'");
	$row=mysqli_fetch_array($result);
	 $pass_encrypt=md5(mysqli_real_escape_string($db,$_POST['defaultcurrentPass']));
	if($pass_encrypt == $row["password"]) {
		$passnew_encrypt=md5(mysqli_real_escape_string($db,$_POST['changenewPass']));
	$str=$db->query("UPDATE users set password='" . $passnew_encrypt . "' WHERE id='" . $_SESSION["userId"] . "'");
	$message = "You have successfully changed your password.";
	} else $message = "Current Password is not correct";
}

?>

HTML Code for Change Password Form

<input type="hidden" value=""name="hofid"/>
function validatePassword() {
var defaultcurrentPass,changenewPass,RetypePassword,output = true;

defaultcurrentPass = document.frmChange.defaultcurrentPass;
changenewPass = document.frmChange.changenewPass;
RetypePassword = document.frmChange.RetypePassword;

if(!defaultcurrentPass.value) {
	defaultcurrentPass.focus();
	document.getElementById("defaultcurrentPass").innerHTML = "required";
	output = false;
}
else if(!changenewPass.value) {
	changenewPass.focus();
	document.getElementById("changenewPass").innerHTML = "required";
	output = false;
}
else if(!RetypePassword.value) {
	RetypePassword.focus();
	document.getElementById("RetypePassword").innerHTML = "required";
	output = false;
}
if(changenewPass.value != RetypePassword.value) {
	changenewPass.value="";
	RetypePassword.value="";
	changenewPass.focus();
	document.getElementById("RetypePassword").innerHTML = "Both Password are not same";
	output = false;
} 	
return output;
}

CSS Code For Display Message and Validation

.required {
color: #FF0000;
font-size:16px;
font-weight:italic;
padding-left:10px;
}

.message {
color: #FF0000;
text-align: center;
width: 100%;
}

View Demo

Leave a Comment