Change Password in php Script

Hi guys,

Change Password In Php Script .if you are developing PHP website or project then you need to add change password function on your website. So here i will give you change a password using md5, a session in PHP Mysql. we will use MySQL as a database. here I give you very short code and smart way to do change password in your working project.

index.php

<?php


$_SESSION["userId"] = 31;


if(isset($_POST['updatepass']))
{
 $result =$db->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";
}


?>


<!doctype html>
<html lang="en">
<head>
 <style type="text/css">
 .required {
 color: #FF0000;
 font-size:16px;
 font-weight:italic;
 padding-left:10px;
 }

 .message {
 color: #FF0000;
 text-align: center;
 width: 100%;
 }
 </style>
</head>
<body>
 <form name="frmChange" action="" method="POST" onSubmit="return validatePassword()">
 <div class="form-group">
 <label>Current Password: <span id="defaultcurrentPass"  class="required"></span></label>


 <input type="password" name="defaultcurrentPass" class="form-control"/>
 </div>
 <div class="form-group">
 <label>New Password: <span id="changenewPass" class="required"></span></label>
 <input type="password" name="changenewPass" class="form-control"/>


 </div>
 <div class="form-group">
 <label>Retype New Password: <span id="RetypePassword" class="required"></span></label>


 <input type="password" name="RetypePassword" class="form-control"/>
 </div>


 <div>
 <input type="hidden" value="<?php echo $cms_desc['ofid']; ?>"name="hofid"/>
 <input type="submit" name="updatepass"  class="btn btn-primary" value="Update Password"/>


 </div>
 </form>
</body>


<script type="text/javascript">
 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;
 }
</script>
</html>

i hope you found your best solution…