I've been following this tutorial by phpacademy on OOP Login and Register functions and I cant seem to be able to get the remember me function to work. I think the problem is that $user-> login();
doesnt pass any data but i cant seem to be able to fix it.
and here is a part of my init.php
if (Cookie::exists(Config::get('remember/cookie_name')) && !Session::exists(Config::get('session/session_name'))) {
$hash = Cookie::get(Config::get('remember/cookie_name'));
$hashCheck = DB::getInstance()->get('users_session', array('hash', '=', $hash));
if ($hashCheck->count()) {
$user = new User($hashCheck->first()->user_id);
$user-> login();
}
}
this is my User.php file
public function login($username = null, $password= null, $remember = false) {
if (!$username && !$password && $this->exists()) {
//problematic if statement removing $this->exists() gives me the error 'Trying to get property of non-object'
Session::put($this->_sessionName, $this->data()->id);
} else {
$user = $this->find($username);
if ($user) {
if ($this->data()->password === Hash::make($password, $this->data()->salt)) {
Session::put($this->_sessionName, $this->data()->id);
if ($remember) {
$hash = Hash::unique();
$hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
if (!$hashCheck->count()) {
$this->_db->insert('users_session', array(
'user_id' => $this->data()->id,
'hash' => $hash
));
} else {
$hash = $hashCheck->first()->hash;
}
Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
}
return true;
}
}
}
return false;
}
public function exists() {
return (!empty($this->_data)) ? true : false;
}