2

This code is in my user.php file

include('password.php');
class User extends Password{

        private $_db;

        function __construct($db){
            parent::__construct();

            $this->_db = $db;
        }

        private function get_user_hash($email){ 

            try {
                $stmt = $this->_db->prepare('SELECT password FROM users WHERE email = :email AND active="Yes" ');
                $stmt->execute(array('email' => $email));

                $row = $stmt->fetch();
                return $row['password'];

            } catch(PDOException $e) {
                echo '<p class="bg-danger">'.$e->getMessage().'</p>';
            }
        }

        public function login($email,$password){

            $hashed = $this->get_user_hash($email);

            if($this->password_verify($password,$hashed) == 1){

                $_SESSION['loggedin'] = true;
                return true;
            }   
        }

        public function logout(){
            session_destroy();
        }

        public function is_logged_in(){
            if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
                return true;
            }       
        }
    }

And this code is part of my login.php file

function login($email,$password){

$hashed = $this->get_user_hash($email);  <--the error is in this line

if($this->password_verify($password,$hashed) == 1){

    $_SESSION['loggedin'] = true;
    return true;
}     

I honestly don't know what to do, I'm still learning these kinds of things & I'm not really good with classes and objects that's why I'm asking everyone to help me fix this problem.

Suhaib Janjua
  • 3,452
  • 14
  • 56
  • 72

1 Answers1

1

Replace this line

$hashed = $this->get_user_hash($email);  <--the error is in this line

With

$user = new User($db);
$hashed = $user->get_user_hash($email);

You can not use this outside a class. You can learn more about $this in php in following SO post. What does the variable $this mean in PHP?

Community
  • 1
  • 1
chanchal118
  • 3,339
  • 2
  • 25
  • 50