I've a db with crypted password. When a user logs in, i make this:
$result = mysqli_fetch_assoc(mysqli_query($conn,$query));
$cryptedPass = $result['password'];
$pass = $_POST['password'];
if(strcmp($cryptedPass,md5($pass))==0)
echo "yeah!";
It works, but I would like to know if this the right manner, or if there is something of safer!
Don't use MD5. There are plenty of online documents that explain how insecure this is. For example:
https://en.wikipedia.org/wiki/MD5
I would recommend using the
crypt()
function.Read here: http://php.net/crypt
A good one to use would be
CRYPT_BLOWFISH
Here's a function I found a while back, that I use. Unfortunately I can't remember where I found it, so I can't reference the author.
To create the encrypted password, you would use it like so:
Then to compare, you would use:
Note: If you are using a version of PHP before 5.3.7, the salt prefix must be
$2a$
.