Ubiquity  2.0.0
php rapid development framework
USession.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\http;
4 
6 
12 class USession {
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  return $value;
127  }
128 
133  public static function delete($key) {
134  self::start();
135  unset($_SESSION[$key]);
136  }
137 
144  public static function inc($key, $inc=1) {
145  return self::set($key, self::get($key, 0) + $inc);
146  }
147 
154  public static function dec($key, $dec=1) {
155  return self::set($key, self::get($key, 0) - $dec);
156  }
157 
162  public static function start($name=null) {
163  if (!isset($_SESSION)) {
164  if (isset($name) && $name !== "") {
165  self::$name=$name;
166  }
167  if (isset(self::$name)) {
168  \session_name(self::$name);
169  }
170  \session_start();
171  }
172  }
173 
178  public static function isStarted() {
179  return isset($_SESSION);
180  }
181 
187  public static function exists($key) {
188  self::start();
189  return isset($_SESSION[$key]);
190  }
191 
195  public static function terminate() {
196  if (!self::isStarted())
197  return;
198  self::start();
199  $_SESSION=array ();
200 
201  if (\ini_get("session.use_cookies")) {
202  $params=\session_get_cookie_params();
203  \setcookie(\session_name(), '', \time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
204  }
205  \session_destroy();
206  }
207 }
Http Session utilities.
Definition: USession.php:12
static dec($key, $dec=1)
Decrement the value at the key index in session.
Definition: USession.php:154
static exists($key)
Returns true if the key exists in Session.
Definition: USession.php:187
static session($key, $default=NULL)
Returns the value stored at the key position in session.
Definition: USession.php:103
static isStarted()
Returns true if the session is started.
Definition: USession.php:178
static getBoolean($key)
Returns a boolean stored at the key position in session.
Definition: USession.php:88
static terminate()
Terminates the active session.
Definition: USession.php:195
static setBoolean($key, $value)
Sets a boolean value at key position in session.
Definition: USession.php:78
static start($name=null)
Start new or resume existing session.
Definition: USession.php:162
static inc($key, $inc=1)
Increment the value at the key index in session.
Definition: USession.php:144
static removeValueFromArray($arrayKey, $value)
Removes a value from an array in session.
Definition: USession.php:58
static getArray($arrayKey)
Returns an array stored in session variable as $arrayKey.
Definition: USession.php:20
static addValueToArray($arrayKey, $value)
Adds a value from an array in session.
Definition: USession.php:68
static addOrRemoveValueFromArray($arrayKey, $value, $add=true)
Adds or removes a value from an array in session.
Definition: USession.php:38