Praba
Praba

Reputation: 67

How to compare md5 password php

I need to check if $user_entered_passidential to $hash_val . Please help me?

    <?php
    $mypass = "Rainbow";
    $hash_val = md5($mypass);

    echo $hash_val; //always 32 characters

    $user_entered_pass = "rainbow";
?>

Upvotes: 2

Views: 20180

Answers (3)

Andreas
Andreas

Reputation: 2837

Compare it with the identical operator ===, e.g.:

if ($hashFromDatabase === md5($userEnteredPasswort)) {
    // authenticated ...
}

However, I strongly recommend you not to use md5 as hashing algorithm. Check out this answer here: Secure hash and salt for PHP passwords

Moreover, I recommend you using the new password hashing API from PHP (password_hash() and password_verify()).

Upvotes: 2

Miguel Jim&#233;nez
Miguel Jim&#233;nez

Reputation: 514

You can hash $user_entered_pass and compare, I use this method.

<?php
    $mypass = "Rainbow";
    $hash_val = md5($mypass);

    echo $hash_val; //always 32 characters

    $user_entered_pass = "rainbow";

    if(md5($user_entered_pass) == $hash_val){
      //The passwords are equal
    }
?>

Upvotes: 11

Bikash
Bikash

Reputation: 1938

Convert user entered password to md5 and then check. $user_entered_pass_has = md5($user_entered_pass) Then check for equality.

Upvotes: 2

Related Questions