Change Password PHP and Mysqli Script - Mostlikers

10 July, 2013

Change Password PHP and Mysqli Script

Hi guys, Today we are going to discuss how to change the password using PHP and MySQLi script. Change password is an important feature for a web application. registered user can able to change they are old password anytime. It makes a user protect the sensitive pages from hackers.

Change Password PHP and Mysqli Script


Live demo               Download

Logic

  • First, you have created HTML form elements with respective text-filed(old password,new password,confirm password).
  • Check the basic validation whether the new password and confirm password is same or not and empty fields validation.
  • After the validation, you have to check old password valid or not. The password is the match then allow to next level.
  • If the old password and new password is same don't allow the change the password.


Database

Create sample table name called 'signup' insert manually username,email id, Password(hash key value).

CREATE TABLE `singup` (
`username` VARCHAR( 25 ) NOT NULL , 
`email` VARCHAR( 25 ) NOT NULL , 
`password` VARCHAR( 25 ) NOT NULL
) 
INSERT INTO users VALUES ('user', 'you@example.com', 'sds34axcv3');


PHP

 If the old password matches with signup table then the new password will upload the database table.
<?php
 $db = new mysqli('localhost', 'root', '', 'db_name');
  if(isset($_POST['submit'])):
  extract($_POST);
  if($old_password!="" && $password!="" && $confirm_pwd!="") :
  $user_id = '1';// sesssion id
  $old_pwd=md5(mysqli_real_escape_string($db,$_POST['old_password']));
  $pwd=md5(mysqli_real_escape_string($db,$_POST['password']));
  $c_pwd=md5(mysqli_real_escape_string($db,$_POST['confirm_pwd']));
  if($pwd == $c_pwd) :
  if($pwd!=$old_pwd) :
    $sql="SELECT * FROM `singup` WHERE `id`='$user_id' AND `password` ='$old_pwd'";
    $db_check=$db->query($sql);
    $count=mysqli_num_rows($db_check);
  if($count==1) :
    $fetch=$db->query("UPDATE `singup` SET `password` = '$pwd' WHERE `id`='$user_id'");
    $old_password=''; $password =''; $confirm_pwd = '';
    $msg_sucess = "Your new password update successfully.";
  else:
    $error = "The password you gave is incorrect.";
  endif;
  else :
    $error = "Old password new password same Please try again.";
  endif;
  else:
    $error = "New password and confirm password do not matched";
  endif;
  else :
    $error = "Please fil all the fields";
  endif;   
  endif;
?> 


HTML

Create simple form element. 
<form method="post" autocomplete="off" id="password_form">
<p>old password<br />
 <input type="password" name="old_password" /></p>
<p>New password<br />
 <input type="password" name="password"  id="password" class="ser" />
</p>
<p>Confirm password<br />
 <input type="password" name="confirm_pwd" id="confirm_pwd" class="ser" />
</p>
<p align="center">
 <input name="submit" type="submit" value="Save Password" class="submit" />
</p>
<div class="<?=(@$msg_sucess=="") ? 'error' : 'green' ; ?>" id="logerror">
 <?php echo @$error; ?><?php echo @$msg_sucess; ?>
</div>
</form>

CSS

Add style code for output message.
<style type="text/css">
.error{
margin-top: 6px;
margin-bottom: 0;
color: #fff;
background-color: #D65C4F;
display: table;
padding: 5px 8px;
font-size: 11px;
font-weight: 600;
line-height: 14px;
  }
.green{
margin-top: 6px;
margin-bottom: 0;
color: #fff;
background-color: green;
display: table;
padding: 5px 8px;
font-size: 11px;
font-weight: 600;
line-height: 14px;
  }
</style>

"Let us sacrifice our today so that our children can have a better tomorrow."
                           - A. P. J. Abdul Kalam








Related Topics

6 comments: