summaryrefslogtreecommitdiff
path: root/include/MySession.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/MySession.class.php')
-rw-r--r--include/MySession.class.php61
1 files changed, 61 insertions, 0 deletions
diff --git a/include/MySession.class.php b/include/MySession.class.php
new file mode 100644
index 0000000..b57f7c8
--- /dev/null
+++ b/include/MySession.class.php
@@ -0,0 +1,61 @@
+<?php
+
+class MySession {
+
+ private static $_initiated = FALSE;
+ private static $_dataContainer = array();
+
+ public static function init() {
+ if (!self::$_initiated) {
+ session_start();
+ if (!isset($_SESSION['__data'])) {
+ $_SESSION['__data'] = [];
+ }
+ self::$_dataContainer = &$_SESSION['__data'];
+ }
+ self::$_initiated = TRUE;
+ }
+
+ private static function _getRealm() {
+ $backtrace = debug_backtrace();
+ while (count($backtrace) && isset($backtrace[0]['class']) && $backtrace[0]['class'] == get_class()) {
+ array_shift($backtrace);
+ }
+ if (!count($backtrace) || (!isset($backtrace[0]['class']))) {
+ throw new SessionRealmException('Session realm not provided');
+ }
+ return $backtrace[0]['class'];
+ }
+
+ public static function get($variable, $realm = NULL) {
+ if (!self::$_initiated) {
+ self::init();
+ }
+ if (!$realm) {
+ $realm = self::_getRealm();
+ }
+ if (!isset(self::$_dataContainer[$realm])) {
+ throw new SessionVariableException('Realm '.$realm.' doesn\'t exist');
+ }
+ if (!isset(self::$_dataContainer[$realm][$variable])) {
+ throw new SessionVariableException('Variable '.$realm.'::'.$variable.' not set');
+ }
+ return self::$_dataContainer[$realm][$variable];
+ }
+
+ public static function set($variable, $value, $realm = NULL) {
+ if (!self::$_initiated) {
+ self::init();
+ }
+ if (!$realm) {
+ $realm = self::_getRealm();
+ }
+ if (!isset(self::$_dataContainer[$realm])) {
+ self::$_dataContainer[$realm] = [];
+ }
+ return self::$_dataContainer[$realm][$variable] = $value;
+ }
+
+}
+
+?>