chardenlada
New Member
i've implemented this custom PHP Session Class for storing sessions into a MySQL database:\[code\]class Session{ private $_session; public $maxTime; private $database; public function __construct(mysqli $database) { $this->database=$database; $this->maxTime['access'] = time(); $this->maxTime['gc'] = get_cfg_var('session.gc_maxlifetime'); session_set_save_handler(array($this,'_open'), array($this,'_close'), array($this,'_read'), array($this,'_write'), array($this,'_destroy'), array($this,'_clean') ); register_shutdown_function('session_write_close'); session_start();//SESSION START } public function _open() { return true; } public function _close() { $this->_clean($this->maxTime['gc']); } public function _read($id) { $getData= http://stackoverflow.com/questions/2006064/$this->database->prepare("SELECT data FROM Sessions AS Session WHERE Session.id = ?"); $getData->bind_param('s',$id); $getData->execute(); $allData= http://stackoverflow.com/questions/2006064/$getData->fetch(); $totalData = count($allData); $hasData=(bool) $totalData >=1; return $hasData ? $allData['data'] : ''; } public function _write($id, $data) { $getData = http://stackoverflow.com/questions/2006064/$this->database->prepare("REPLACE INTO Sessions VALUES (?, ?, ?)"); $getData->bind_param('sss', $id, $this->maxTime['access'], $data); return $getData->execute(); } public function _destroy($id) { $getData=http://stackoverflow.com/questions/2006064/$this->database->prepare("DELETE FROM Sessions WHERE id = ?"); $getData->bind_param('S', $id); return $getData->execute(); } public function _clean($max) { $old=($this->maxTime['access'] - $max); $getData = http://stackoverflow.com/questions/2006064/$this->database->prepare("DELETE FROM Sessions WHERE access < ?"); $getData->bind_param('s', $old); return $getData->execute(); }}\[/code\]It works well but i don't really know how to properly access the \[code\]$_SESSION\[/code\] array:For example: \[code\]$db=new DBClass();//This is a custom database class$session=new Session($db->getConnection());if (isset($_SESSION['user'])){ echo($_SESSION['user']);//THIS IS NEVER EXECUTED!}else{ $_SESSION['user']="test"; Echo("Session created!");}\[/code\]At every page refresh it seems that \[code\]$_SESSION['user']\[/code\] is somehow "resetted", what methods can i apply to prevent such behaviour?