Skip to main content
3 of 3
added 471 characters in body

Custom session class

<?php
/**
*session.php
*danutz0501
*@copyright 2013
**/
//defined('BASE_PATH')||(header("HTTP/1.1 403 Forbidden")&die('403 - Acces direct interzis.'));
class session{

    private $db, $table_name = 'session_data', $session_name = 'PDOSESSID', $session_litetime = 1440;
    private static $instance = null;


    public static function init(PDO $pdo, $table_name = null, $session_name = null, $session_lifetime = null){
        if(self::$instance == null)
            self::$instance = new self($pdo, $table_name, $session_name, $session_lifetime);
        return self::$instance;
    }

    private function __construct($pdo, $table_name, $session_name, $session_lifetime){
        $this->db               = $pdo;
        $this->table_name       = (is_null($table_name))       ? $this->table_name       : $table_name;
        $this->session_name     = (is_null($session_name))     ? $this->session_name     : $session_name;
        $this->session_litetime = (is_null($session_lifetime)) ? $this->session_litetime : $session_lifetime;
        session_set_save_handler(
            [$this, 'open'], [$this, 'close'], [$this, 'read'], [$this, 'write'], [$this, 'destroy'], [$this, 'gc']
        );
        register_shutdown_function('session_write_close');
        ini_set('session_auto_start', 0);
        ini_set('session.cookie_httponly',1);
        ini_set('session.use_only_cookies',1);
        ini_set('session.gc_probability', 25);
        ini_set('session.gc_divisor', 100);
        ini_set('session.gc_maxlifetime', $this->session_litetime);
        session_name($this->session_name);
        session_start();
        $this->regenerate_id();
    }

    private function __clone(){}

    public function open($save_path, $session_name){
        return true;
    }

    public function close(){
        $this->gc($this->session_litetime);
        return true;
    }

    public function read($session_id){
       $session = $this->fetch_session($session_id);
        return ($session === false) ? false : $session['data'];
    }

    public function write($session_id, $session_data){
        $session = $this->fetch_session($session_id);
        if($session === false)
            $stmp = $this->db->prepare('INSERT INTO '.$this->get_table().' (id, data, unixtime) VALUES (:id, :data, :time)');
        else
            $stmp = $this->db->prepare('UPDATE '.$this->get_table().' SET data = :data, unixtime = :time WHERE id = :id');
        $stmp->execute([':id' => $session_id, ':data' => $session_data, ':time' => time()]);
    }

    public function destroy($session_id){
        $stmp = $this->db->prepare('DELETE FROM '.$this->get_table().' WHERE id = :id');
        $stmp->execute([':id' => $session_id]);
    }

    public function gc($session_lifetime){
        $stmp = $this->db->prepare('DELETE FROM '.$this->get_table().' WHERE unixtime < :time');
        $stmp->execute([':time' => (time() - (int) $session_lifetime)]);
    }

    private function get_table(){
        $table = $this->db->quote($this->table_name);
        return ($table) ? $table : $this->table_name;
    }

    private function regenerate_id(){
        $old_session = session_id();
        session_regenerate_id();
        $new_session = session_id();
        $stmp = $this->db->prepare('UPDATE '.$this->get_table().' SET id = :new_id WHERE id = :old_id');
        $stmp->execute([':new_id' => $new_session, ':old_id' => $old_session]);
    }

    private function fetch_session($session_id){
        $smtp = $this->db->prepare("SELECT id, data FROM ".$this->get_table()." WHERE id = :id AND unixtime > :unixtime");
        $smtp->execute(['id' => $session_id, 'unixtime' => (time() - (int) $this->session_litetime)]);
        $session = $smtp->fetchAll();
        return empty($session) ? false : $session[0];
    }

    public static function set($key, $value){
        if(!self::check_dot($key))
            $_SESSION[$key] = $value;
        else
            self::set_dot($_SESSION, $key, $value);
    }

    public static function get($key){
        if(!self::check_dot($key))
            return (isset($_SESSION[$key])) ? $_SESSION[$key] : false;
        else
            return self::get_dot($_SESSION, $key);
    }

    private static function set_dot(&$root, $key, $value){
        $keys = explode('.', $key);
        while(count($keys) > 1) {
            $key1 = array_shift($keys);
            if(!isset($root[$key1])) {
                $root[$key1] = [];
            }
            $root = &$root[$key1];
        }
        $key1 = reset($keys);
        $root[$key1] = $value;
    }

    private static function get_dot(&$root, $key){
        foreach(explode('.', $key) as $key1){
            if(!is_array($root) || !array_key_exists($key1, $root))
                return false;
            $root = &$root[$key1];
        }
        return $root;
    }

    public static function delete(){
        $_SESSION = [];
        if(isset($_COOKIE[session_name()])){
            $params = session_get_cookie_params();
            setcookie(session_name(), '', 1, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
        }
        session_destroy();
    }

    private static function check_dot($val){
        return (bool) (strpos($val, '.')) ? true : false;
    }

    public static function check_data($key, $val){
        foreach($key as $k){
            if(!array_key_exists($k, $_SESSION) || $_SESSION[$k] != array_shift($val))
                return false;
        }
        return true;
    }
}

I just want some opinions about my session class. How bad is it? What can I improve? What's wrong about my class? I am trying to use a dot syntax for writing multi-dimensional arrays. EDIT: including some calling code, really simple

$pdo = new PDO('sqlite:data.db');
$pdo->exec('CREATE TABLE IF NOT EXISTS session_data(id INT, data TEXT, unixtime INT)');
include 'session.php';
session::init($pdo);
session::set('abc1.abc2.abc3.abc4.abc5','test');
echo session::get('abc1.abc2.abc3.abc4.abc5');
session::set('abc1.abc2.abc31', 45);
echo '<br />'.session::get('abc1.abc2.abc31').'<pre>';
print_r($_SESSION);