Php 检查密码时,它只关心前8个字符

Php 检查密码时,它只关心前8个字符,php,mysql,passwords,crypt,password-confirmation,Php,Mysql,Passwords,Crypt,Password Confirmation,我有一个注册表单,创建一个配置文件和一个“编辑配置文件”页面,允许用户更改信息,只要他们也输入了密码 问题是“编辑配置文件”页面只确保密码的前8个字符匹配。或者,实际上,我想更好的描述应该是crypt()函数只编码前8个字符 这是我的注册表格代码: $password = $_REQUEST['password']; // Checks to make sure that the password isn't blank if (!$password) {

我有一个注册表单,创建一个配置文件和一个“编辑配置文件”页面,允许用户更改信息,只要他们也输入了密码

问题是“编辑配置文件”页面只确保密码的前8个字符匹配。或者,实际上,我想更好的描述应该是crypt()函数只编码前8个字符

这是我的注册表格代码:

    $password = $_REQUEST['password'];

    // Checks to make sure that the password isn't blank
    if (!$password) {
          handle_error("You have left the password field blank.", "Blank Password"); 
    //handle_error() is a custom function
    }
下面是我将其插入MySQL数据库的代码:

    $insert_sql = sprintf("INSERT INTO users (first_name, last_name, pen_name, website, password, email, user_bio, user_reason, hash, profile_pic) " .
            "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
            mysql_real_escape_string($first_name),
            mysql_real_escape_string($last_name),
            mysql_real_escape_string($pen_name),
            mysql_real_escape_string($website),
            mysql_real_escape_string(crypt($password, $email)),
            mysql_real_escape_string($email),
            mysql_real_escape_string($bio), 
            mysql_real_escape_string($reason), 
            mysql_real_escape_string($hash), 
            mysql_real_escape_string($upload_filename));
“hash”变量的用途与密码无关,所以忽略它

这是“编辑配置文件”代码

我对PHP比较陌生,但所有代码都是我自己写的,所以如果有人需要任何澄清,我很乐意解释自己

正如我所说,问题是我的页面只关心密码的前8个字符。当我试图用错误的密码验证“编辑配置文件”时,它会给出一个错误。如果我将密码留空,它将给出一个错误。但是如果我正确地输入了前8个字符,然后输入了一堆胡言乱语,它就会接受密码!我不确定是否有crypt()函数或字符串我没有得到

哦,我正在做(!($string1===$string2)),但是我已经尝试了strcomp()函数,并且以同样的方式工作

非常感谢

来自:

标准的基于DES的crypt()返回salt作为前两个 输出的字符。它也只使用前八个字符 对于str,因此以相同的八个字符开头的更长字符串 将产生相同的结果(使用相同的盐时)

您应该尝试另一种算法以获取哈希值。你可以在链接中找到不同的例子


希望它有帮助

您是否尝试打印字符串以便知道是否只显示8个字符?另外,密码字段的类型是什么?PHP提供了一个专门用于哈希密码的函数:password_hash()从其他一些问题中选择:你确定
$email
变量有一个正确的值吗?我有点好奇为什么你要将email用作salt…非常感谢!我将使用另一个哈希函数。@user3798761-该函数真的很有用。
    $password_query = "SELECT * FROM users WHERE user_id = " . $user_id;    
            $password_query_run = mysql_query($password_query); 
            $password_array = mysql_fetch_array($password_query_run);   
            $password_old_hash = $password_array['password'];   
            $password_new_hash = crypt($password, $email);  
            if(!($password_new_hash === $password_old_hash)) {
                    handle_error("The Password you entered does match what we have in our system.","Wrong Password.");
            }