PHP & MySQLi OOP - Session value not being set? -
when logging user in have checked the submitted values being set, (tested var_dump), validation of checking inputed values against database works fine redirect index.php page works fine, session value not being set "not logged in" message being shown @ top of page implemented see if code worked, should saying "logged in" when redirected index.php page. result of var_dump on session of userid null. should users id.
login.php:
<?php require_once('includes/classes/session.php'); require_once('includes/classes/database.php'); require_once('includes/classes/user.php'); require_once('includes/functions/general.php'); require_once('includes/functions/security.php'); require_once('includes/functions/user.php'); if($session->isloggedin()) { redirect('index.php'); } if (isset($_post['submit'])) { $username = trim($_post['username']); $password = trim($_post['password']); $founduser = user::verify($username, $password); if ($founduser) { $session->login($founduser); redirect('index.php'); } else { $error = "combination incorrect"; } } ?>
session.php:
<?php class session { private $loggedin; public $userid; function __contruct() { session_start(); $this->checklogin(); } public function isloggedin() { return $this->loggedin; } private function checklogin() { if(isset($_session['userid'])) { $this->userid = $_session['userid']; $this->loggedin = true; } else { unset($this->userid); $this->loggedin = false; } } public function login($user) { if($user) { $this->userid = $_session['userid'] = $user->userid; $this->loggedin = true; } } public function logout() { unset($_session['userid']); unset($this->userid); $this->loggedin = false; } } $session = new session(); ?>
user.php:
<?php class user { public $userid; public $username; public $password; public $email; public $firstname; public $lastname; public $access; public $active; public static function getusers() { return self::getbysql("select * users"); } public static function getuserid($id=0) { global $db; $resultarray = self::getbysql("select * users userid={$id}"); return !empty($resultarray) ? array_shift($resultarray) : false; } public static function getbysql($sql) { global $db; $result = $db->query($sql); $objarray = array(); while ($row = $db->fetcharray($result)) { $objarray[] = self::instantiate($row); } return $objarray; } public function getname() { if (isset($this->firstname) && isset($this->lastname)) { return $this->firstname . " " . $this->lastname; } else { return ""; } } private static function instantiate($record) { $object = new self; foreach($record $attr=>$value){ if($object->hasattr($attr)) { $object->$attr = $value; } } return $object; } private function hasattr($attr) { $objectvars = get_object_vars($this); return array_key_exists($attr, $objectvars); } public static function verify($username, $password) { global $db; $username = $db->prepare($username); $password = $db->prepare($password); $sql = "select * users username = '{$username}' , userpass = '{$password}'"; $resultarray = self::getbysql($sql); return !empty($resultarray) ? array_shift($resultarray) : false; } } ?>
Comments
Post a Comment