PHP OOP remember me function error -


i've been following tutorial phpacademy on oop login , register functions , cant seem able remember me function work. think problem $user-> login(); doesnt pass data cant seem able fix it.

and here part of 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 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 error  'trying 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; } 

these (partial)files work me:

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();     } } 

user.php: (the complete file have)

<?php     class user {         private $_db,                 $_data,                 $_sessionname,                 $_cookiename,                 $_isloggedin;          public function __construct($user = null) {             $this->_db = db::getinstance();              $this->_sessionname = config::get('session/session_name');             $this->_cookiename = config::get('remember/cookie_name');              if(!$user) {                 if(session::exists($this->_sessionname)) {                     $user = session::get($this->_sessionname);                      if($this->find($user)) {                         $this->_isloggedin = true;                     } else {                      }                 }             } else {                 $this->find($user);             }         }          public function update ($fields = array(), $id = null) {             if(!$id && $this->isloggedin()) {                 $id = $this->data()->id;             }              if(!$this->_db->update('users', $id, $fields)) {                 throw new exception('there problem updating');             }         }          public function create($fields) {             if(!$this->_db->insert('users', $fields)) {                 throw new exception('there problem creating account');             }         }         public function find($user = null) {             if($user) {                 $field = (is_numeric($user)) ? 'id' : 'username';                 $data = $this->_db->get('users', array($field, '=', $user));                  if($data->count()) {                     $this->_data = $data->first();                     return true;                 }             }             return false;         }          public function login($username = null, $password = null, $remember = false) {              if(!$username && !$password && $this->exists()) {                 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 haspermission($key) {             $group = $this->_db->get('groups', array('id', '=', $this->data()->group));              if($group->count()) {                 $permissions = json_decode($group->first()->permissions, true);                  if($permissions[$key] == true) {                     return true;                 }             }             return false;         }          public function exists() {             return (!empty($this->_data)) ? true : false;         }          public function logout() {             $this->_db->delete('users_session', array('user_id', '=', $this->data()->id));             session::delete($this->_sessionname);             cookie::delete($this->_cookiename);         }          public function data() {             return $this->_data;         }          public function isloggedin() {             return $this->_isloggedin;         }     } ?> 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -