我的登录功能无法匹配存储在数据库中的用户名和密码

问题描述 投票:1回答:1

此代码总是返回错误的用户名和密码,虽然它的正确和相同的功能适用于我的管理员登录系统。它工作了一段时间,现在它不会。

function login($user,$pass,$accountType){
require('connection.php');

$result=mysqli_query($con,"select * From $accountType");
    $regex="/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/";
    if(preg_match($regex, $user)){
        if(mysqli_num_rows($result)>0){
            while($row=mysqli_fetch_assoc($result)){
                $username=$row['username'];
                $password=$row['password'];
                if($user==$username){   
                        if (password_verify($pass,$password)){                               
                            if($accountType=='student'){
                                $_SESSION['student']='$user';
                                header('Location:studentprofile.php');

                                exit();
                            }
                            elseif($accountType=='teacher'){
                                $_SESSION['student']='$user';
                                header('Location:teacherprofile.php');
                                exit();
                                }
                            elseif($accountType=='admin'){
                                header('Location:admin.html');
                                exit();
                            }
                        }
                        else
                        {
                            $_SESSION['error']="wrong password"."<br>";
                        }
                }
                else{
                    $_SESSION['error']="Wrong username "."<br>";
                }   
            }

        }
        else{
            $_SESSION['error']="No data found";
        }
    }
    else{
        $_SESSION['error']="not a valid email,";
    }

}
php password-hash
1个回答
2
投票

来自comments“我已指定varchar(20)用于在数据库中存储密码”

密码字段应该很大。 password_hash()可以生成一些非常冗长的文本(当前默认值为60个字符),因此使字段更大,例如varchar(254)text将允许所需的长度。 PHP团队正在为该方法添加更多算法,这意味着哈希可以并且将会增长。我们也不想限制用户使用他们选择的密码或密码的能力。最好留出改变的空间

© www.soinside.com 2019 - 2024. All rights reserved.