简体   繁体   English

PHP MD5 无法正常工作

[英]PHP MD5 not working correctly

So I'm using md5 to encode and decode passwords when users register and login, however the password in my database doesn't match the password used when logging in even though I know they are the same word:所以我在用户注册和登录时使用 md5 对密码进行编码和解码,但是我的数据库中的密码与登录时使用的密码不匹配,即使我知道它们是同一个词:

This is what is in my database这是我的数据库中的内容

098f6bcd4621d373cade4e832 098f6bcd4621d373cade4e832

And this is what using md5 on the password used on the login screen gives me这就是在登录屏幕上使用的密码上使用 md5 给我的

098f6bcd4621d373cade4e832627b4f6 098f6bcd4621d373cade4e832627b4f6

As you can see there are 7 additional characters, but I can't tell why they aren't the same.如您所见,还有 7 个额外的字符,但我不知道为什么它们不一样。 Here is the script used for inserting the user details into the database on registering这是用于在注册时将用户详细信息插入数据库的脚本

$qry =  "INSERT INTO Members(fname, lname, fullname, email, login, passwd, bad_league_id) 
         VALUES('$fname','$lname',0,'$email', '$login','".md5($_POST['password'])."',0)";
$result = @mysql_query($qry);

And here is how my login form compares the values这是我的登录表单比较值的方式

$qry="SELECT * FROM Members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
$result=mysql_query($qry);

On another note, I am currently in the process of transferring over from mysql to mysqli so forgive the use of mysql as I know its deprecated.另一方面,我目前正在从 mysql 转移到 mysqli,因此请原谅使用 mysql,因为我知道它已被弃用。

By default, the hash generated by the MD5() function is 32 characters long. 默认情况下,由MD5()函数生成的哈希长度为32个字符。 The datatype of the passwd column is currently VARCHAR (25) , which means it will only store up to 25 characters, 7 characters less than the actual value. passwd列的数据类型当前为VARCHAR (25) ,这意味着它最多只能存储25个字符,比实际值少7个字符。

To fix this, you will need to change the datatype of the passwd column to be able to store the correct length, eg VARCHAR (32) . 要解决此问题,您将需要更改passwd列的数据类型以能够存储正确的长度,例如VARCHAR (32)

try to use尝试使用

VARCHAR (32) VARCHAR (32)

in the password column在密码栏中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM