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,538
  • 16
  • 59
  • 73

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,551
  • 2
  • 26
  • 52
  • The error changed to "Call to private method User::get_user_hash() from context" Do you know how to fix it? And thanks :) – Ed Ryan Estrella Apr 19 '15 at 06:07
  • You need to declare method as public if want to access outside function. remove `private` keyword from function definition. – chanchal118 Apr 19 '15 at 06:09
  • And now it changed to "Call to a member function prepare() on null" well I don't know what this means. Sorry, I'm really new to this. – Ed Ryan Estrella Apr 19 '15 at 06:16
  • 1
    SO can't replace a good tutorial. Your questions are so basic that it's obivous that a tutorial and basic training on PHP's classes is what you need. Fixing errors one by one through asking here isn't going to help you. – Ulrich Eckhardt Apr 19 '15 at 06:22
  • @EdRyanEstrella your $db object has not been passed correctly. – chanchal118 Apr 19 '15 at 06:28