无法用php类更改密码

时间:2017-05-30 06:14:27

标签: php pdo

如何在这个类上添加更改密码?我正在尝试但是我在change-pass.php

上收到错误

我不熟悉课程如果有人在这里帮助我制作我的更改密码脚本,我将不胜感激。这些代码根本没有更新我得到以下错误

Fatal error: Call to undefined method USER::password_hash() in C:\wamp\www\Login-Signup\profile.php on line 20
第20行

$new_password = $auth_user->password_hash($_POST['password'], PASSWORD_BCRYPT);

user.class.php

<?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(user_name,user_email,user_pass) 
                                                       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 = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM users WHERE user_name=:uname OR user_email=:umail ");
            $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() == 1)
            {
                if(password_verify($upass, $userRow['user_pass']))
                {
                    $_SESSION['user_session'] = $userRow['user_id'];
                    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;
    }
}
?>

这是我的更改密码PHP脚本

require_once("session.php");

    require_once("class.user.php");
    $auth_user = new USER();


    $user_id = $_SESSION['user_session'];

    $stmt = $auth_user->runQuery("SELECT * FROM users WHERE user_id=:user_id");
    $stmt->execute(array(":user_id"=>$user_id));

    $userRow=$stmt->fetch(PDO::FETCH_ASSOC);


    if(isset($_POST['submit'])){

//hash the password
   $new_password = $auth_user->password_hash($_POST['password'], PASSWORD_BCRYPT);

            $stmt = $conn->runQuery('UPDATE users SET user_pass = :user_pass WHERE user_id = '.$user_id.'');            
        $stmt->execute(array(
                     ':user_id' => $new_password             
                  ));



   }

2 个答案:

答案 0 :(得分:0)

错误很明显,你错过了什么,你试图在password_hash类上调用方法USER,这个方法不存在。因此,要么为password_hash创建包装器方法,要么只使用password_hash函数本身。我推荐最后一个

答案 1 :(得分:0)

是的,正如其他贡献者指出的那样,您的USER类中没有名为password_hash()的方法。检查从哪里获得该类,以确保复制了所有功能。或者,您可以在USER类中创建自己的password_hash($ plain_text_password)实现。该方法应返回发送给它的密码的bcrypt哈希值。