1

Hi guys I am implementing a password validation in my project.

Validate Current password in order to change a new password..

here is my query:

$res=mysql_query("SELECT * FROM users WHERE id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
$pass = $userRow['password'];

    if(isset($_POST['update'])){
        $id = $_SESSION['user'];
        $new_pass = md5(mysql_real_escape_string($_POST['new_pass']));
        if($_POST['old_pass'] != $pass){
            ?>
                <script>alert('Wrong Old Password');</script>
             <?php
        }else if(mysql_query("UPDATE users SET password='$new_pass' WHERE id=$id")){
             ?>
                <script>alert('Password Successfully Updated');</script>
             <?php
         }else{
             ?>
               <script>alert('Failed');</script>
             <?php
         }
    }

the alert "Wrong Old Password" always popping out even though I entered the correct old password. so how to fix this?

2 Answers2

1

Are you saving the old password as md5 ? .. if so, you cannot equal the old pass (which is not yet md5 encrypted) with the retrieved pass from database which is md5 encrypted

PLease also as a side note look at this thread explaining why not to use md5

Why not to use MD5

Community
  • 1
  • 1
DTH
  • 1,133
  • 3
  • 16
  • 36
0

Your old password was stored in encrypted format. Try this:

f(md5($_POST['old_pass']) != $pass)
Muhammad Muazzam
  • 2,810
  • 6
  • 33
  • 62
Yuvaraj R
  • 1
  • 2