Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DbSession
- {
- private static $_instance;
- protected static function log($t) {
- $file = fopen('session.log', 'a+');
- fwrite($file, date('d.m.Y H:i:s ') . $t . PHP_EOL);
- fclose($file);
- }
- public static function setup() {
- self::log('setup');
- require_once('models/sessiondata.php');
- $dbSession = new DbSession(true);
- $dbSession->_sessionModel = new sessiondata();
- session_set_save_handler(array($dbSession, 'open'),
- array($dbSession, 'close'),
- array($dbSession, 'read'),
- array($dbSession, 'write'),
- array($dbSession, 'destroy'),
- array($dbSession, 'clean')
- );
- return $dbSession;
- }
- public static function getInstance() {
- if(empty(self::$_instance)) {
- self::log('instance empty');
- self::$_instance = self::setup();
- } else {
- self::log('instance not empty');
- }
- return self::$_instance;
- }
- public function __construct($firstTime = false)
- {
- $this->log('construct');
- if($firstTime == false) {
- return self::getInstance();
- }
- }
- public function close()
- {
- $this->log('close');
- return true;
- }
- public function open($path, $name)
- {
- $this->log('open '. $path . ' / ' . $name);
- if($this->_sessionModel) {
- return true;
- }
- return true;
- }
- public function read($id)
- {
- $this->log('read ' . $id);
- try {
- $session = $this->_sessionModel->fetch($id);
- $this->log($session->data);
- return $session->data;
- } catch( Exception $e ) {
- return '';
- }
- }
- public function write($id, $data)
- {
- $this->log('write');
- try {
- $session = $this->_sessionModel->fetch($id);
- } catch( Exception $e ) {
- $session = new sessiondata();
- }
- $session->data = $data;
- $session->last_access = time();
- $session->ip = $_SERVER['REMOTE_ADDR'];
- $session->user_agent = $_SERVER['HTTP_USER_AGENT'];
- return $session->save(); //true if ok
- }
- public function destroy($id)
- {
- $this->log('destroy');
- return true;
- try {
- $session = $this->_sessionModel->fetch($id);
- $session->delete();
- } catch( Exception $e ) {
- }
- }
- public function clean($lifetime)
- {
- $this->log('clean');
- return true;
- $old = time() - $lifetime;
- $sessions = $this->_sessionModel->fetchAll("last_access < '$old'");
- if($sessions) {
- foreach($sessions as $session) {
- $session->delete();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement