密码验证始终失败

时间:2019-04-02 17:31:48

标签: php sql-server pdo password-hash

当我尝试验证密码时,密码将被散列并进入数据库,每次我回显了要输入的密码,并且数据库中该列的db密码正确大小时便返回

<?php

require_once('dbconfig.php');

class USER
{   

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }

    public function register($uname, $umail, $upass)
    {
        try
        {
            $new_password = password_hash($upass, PASSWORD_DEFAULT);

            $stmt = $this->conn->prepare("INSERT INTO USERS(USERNAME, EMAIL, PASSWORD) VALUES(:uname, :umail, :upass)");

            $stmt->bindparam(":uname", $uname);
            $stmt->bindparam(":umail", $umail);
            $stmt->bindparam(":upass", $new_password);                                        

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }


    public function doLogin($uname, $umail, $upass)
    {
        try
        {
            $stmt = "SELECT USERID, USERNAME, EMAIL, PASSWORD, FIRSTNAME FROM USERS WHERE USERNAME = :uname OR EMAIL = :umail ";
            $stmt = $this->conn->prepare($stmt, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
            $stmt->bindparam(':uname', $uname);
            $stmt->bindparam(':umail', $umail);
            $stmt->execute();
            $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
            $db_password = $userRow['PASSWORD'];

            $sql = "SELECT COUNT(*) FROM USERS WHERE USERNAME = :uname OR EMAIL = :umail";
            $sql = $this->conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
            $sql->bindparam(':uname', $uname);
            $sql->bindparam(':umail', $umail);
            $sql->Execute();
            $row = $sql->fetch(PDO::FETCH_ASSOC);
            if($row == 1)
            {
                if(password_verify($upass, $userRow['PASSWORD']))
                {
                    $_SESSION['USER_SESSION'] = $userRow['USERID'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    public function is_loggedin()
    {
        if(isset($_SESSION['USER_SESSION']))
        {
            return true;
        }
    }

    public function redirect($url)
    {
        header("Location: $url");
    }

    public function doLogout()
    {
        session_destroy();
        unset($_SESSION['USER_SESSION']);
        return true;
    }
}
?>

编辑代码,我已经添加了整个用户类,但它仍返回false数据库中的密码看起来像这样$ 2y $ 10 $ 16aMCo14n.QyON8dFsaFL..6Fi92LuBdWMCI3eAv3WHKJTblJKQ6q db中的列设置为nvarchar(255)不为空

0 个答案:

没有答案