<?php
/**
* FILE: sessions.php
*
* @AUTHOR: LTD shalvasoft
* @AUTHOR: Shalva Kvaratskhelia
* PROJECT: MVC
* VERSION: 1.0.0
*/
class sessions{
const SESSION_STARTED = TRUE;
const SESSION_NOT_STARTED = FALSE;
// The state of the session
private $sessionState = self::SESSION_NOT_STARTED;
// THE only instance of the class
private static $instance;
private function __construct() {}
/**
* Returns THE instance of 'Session'.
* The session is automatically initialized if it wasn't.
*
* @return object
**/
public static function getInstance(){
if ( !isset(self::$instance)){ self::$instance = new self;
}
self::$instance->startSession();
return self::$instance;
}
/**
* (Re)starts the session.
*
* @return bool TRUE if the session has been initialized, else FALSE.
**/
public function startSession(){
if ( $this->sessionState == self::SESSION_NOT_STARTED ){
}
return $this->sessionState;
}
/**
* Stores datas in the session.
* Example: $instance->foo = 'bar';
* @param $name
* @param $value
*/
public function __set( $name , $value ){
$_SESSION[$name] = $value;
}
/**
* Gets datas from the session.
* Example: echo $instance->foo;
*
* @param name Name of the datas to get.
* @return mixed Datas stored in session.
**/
public function __get( $name ){
if ( isset($_SESSION[$name])){ return $_SESSION[$name];
}
}
public function __isset( $name ){
return isset($_SESSION[$name]); }
public function __unset( $name ){
unset( $_SESSION[$name] ); }
/**
* Destroys the current session.
*
* @return bool TRUE is session has been deleted, else FALSE.
**/
public function destroy(){
if ( $this->sessionState == self::SESSION_STARTED ){
return !$this->sessionState;
}
return FALSE;
}
/**
* Close session
*
* @return mixed
*/
public function Close (){
if ( $this->sessionState == self::SESSION_STARTED ){
$this->sessionState = self::SESSION_NOT_STARTED;
}
}
/**
* set session ID
*
* @param $sessionid
*
* @return mixed
*/
public function SetID ($sessionid){
}
}
/**
* validate session id
*
* @param $session_id
*
* @return mixed
*/
public function Validate_id($session_id){
return preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $session_id) > 0; }
/**
* change session key value by session ID
*
* @param $sessionid
* @param $key
* @param $val
*
* @return mixed
* @internal param $session_key
* @internal param $session_val
*
*/
public function Change ($sessionid, $key, $val){
//Get current session
$this->Close();
$this->SetID($sessionid);
$this->startSession();
//Set superglobal value
$_SESSION[$key]=$val;
self::Close();
//Set the before session
$this->startSession();
return $this->sessionState;
}
/**
* Reading session by session ID
*
* @param $sessionid
*
* @param $key
*
* @return mixed
*/
public function Read ($sessionid, $key){
//Get current session
$this->Close();
$this->SetID($sessionid);
$this->startSession();
//Get superglobal value
$value = (isset($_SESSION[$key])) ?
$_SESSION[$key]: null; $this->Close();
//Set the before session
$this->startSession();
return $value;
}
}