Ubiquity  2.0.0
php rapid development framework
Session.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\http;
4 
6 
12 class Session {
13  private static $name;
14 
20  public static function getArray($arrayKey) {
21  self::start();
22  if (isset($_SESSION[$arrayKey])) {
23  $array=$_SESSION[$arrayKey];
24  if (!is_array($array))
25  $array=[ ];
26  } else
27  $array=[ ];
28  return $array;
29  }
30 
38  public static function addOrRemoveValueFromArray($arrayKey, $value, $add=true) {
39  $array=self::getArray($arrayKey);
40  $_SESSION[$arrayKey]=$array;
41  $search=array_search($value, $array);
42  if ($search === FALSE && $add) {
43  $_SESSION[$arrayKey][]=$value;
44  return true;
45  } else {
46  unset($_SESSION[$arrayKey][$search]);
47  $_SESSION[$arrayKey]=array_values($_SESSION[$arrayKey]);
48  return false;
49  }
50  }
51 
58  public static function removeValueFromArray($arrayKey, $value) {
59  return self::addOrRemoveValueFromArray($arrayKey, $value, false);
60  }
61 
68  public static function addValueToArray($arrayKey, $value) {
69  return self::addOrRemoveValueFromArray($arrayKey, $value, true);
70  }
71 
78  public static function setBoolean($key, $value) {
79  $_SESSION[$key]=UString::isBooleanTrue($value);
80  return $_SESSION[$key];
81  }
82 
88  public static function getBoolean($key) {
89  self::start();
90  $ret=false;
91  if (isset($_SESSION[$key])) {
92  $ret=UString::isBooleanTrue($_SESSION[$key]);
93  }
94  return $ret;
95  }
96 
103  public static function session($key, $default=NULL) {
104  self::start();
105  return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
106  }
107 
114  public static function get($key, $default=NULL) {
115  self::start();
116  return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
117  }
118 
124  public static function set($key, $value) {
125  $_SESSION[$key]=$value;
126  }
127 
132  public static function delete($key) {
133  self::start();
134  unset($_SESSION[$key]);
135  }
136 
141  public static function start($name=null) {
142  if (!isset($_SESSION)) {
143  if (isset($name) && $name !== "") {
144  self::$name=$name;
145  }
146  if (isset(self::$name)) {
147  \session_name(self::$name);
148  }
149  \session_start();
150  }
151  }
152 
157  public static function isStarted() {
158  return isset($_SESSION);
159  }
160 
166  public static function exists($key) {
167  self::start();
168  return isset($_SESSION[$key]);
169  }
170 
174  public static function terminate() {
175  if (!self::isStarted())
176  return;
177  self::start();
178  $_SESSION=array ();
179 
180  if (\ini_get("session.use_cookies")) {
181  $params=\session_get_cookie_params();
182  \setcookie(\session_name(), '', \time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
183  }
184  \session_destroy();
185  }
186 }
static exists($key)
Returns true if the key exists in Session.
Definition: Session.php:166
static addOrRemoveValueFromArray($arrayKey, $value, $add=true)
Adds or removes a value from an array in session.
Definition: Session.php:38
static removeValueFromArray($arrayKey, $value)
Removes a value from an array in session.
Definition: Session.php:58
static terminate()
Terminates the active session.
Definition: Session.php:174
static isStarted()
Returns true if the session is started.
Definition: Session.php:157
static getBoolean($key)
Returns a boolean stored at the key position in session.
Definition: Session.php:88
static addValueToArray($arrayKey, $value)
Adds a value from an array in session.
Definition: Session.php:68
Session utilities.
Definition: Session.php:12
static getArray($arrayKey)
Returns an array stored in session variable as $arrayKey.
Definition: Session.php:20
static session($key, $default=NULL)
Returns the value stored at the key position in session.
Definition: Session.php:103
static start($name=null)
Start new or resume existing session.
Definition: Session.php:141
static setBoolean($key, $value)
Sets a boolean value at key position in session.
Definition: Session.php:78