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. A 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

14 comments:

  1. PASSWORD MD5 MEANS HOW TO CHECK

    ReplyDelete
    Replies
    1. MD5 hash function. it will encrypt the password

      Delete
    2. Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in D:\xampp\htdocs\jewellery\html\admin\setting.php on line 15

      Delete
    3. Result is null add some data in to the table.

      Delete
  2. bro one dout I cannot login after resetting the encrypted password through the PHP code, but I can login before resetting encrypt PWD?

    ReplyDelete
    Replies
    1. Check your select row query there maybe you forget to use md5()

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. wow...! It worked like a charm. Thanks for the tut..

    ReplyDelete