mirror of
https://github.com/sigmasternchen/PHPTools
synced 2025-03-15 07:48:55 +00:00
added virtual session class
This commit is contained in:
parent
9a614a5c20
commit
b53184194f
1 changed files with 66 additions and 0 deletions
66
modules/general/VirtualSessionManager.php
Normal file
66
modules/general/VirtualSessionManager.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
define("MAX_VSESSIONS", 15);
|
||||
|
||||
class vSession {
|
||||
private $name;
|
||||
public function __construct($name) {
|
||||
$this->name = $name;
|
||||
}
|
||||
public function init() {
|
||||
global $_SESSION;
|
||||
if (!isset($_SESSION['vSession']))
|
||||
$_SESSION['vSession'] = array();
|
||||
if (count($_SESSION['vSession']) > MAX_VSESSIONS)
|
||||
$this->deleteOldest();
|
||||
if (!isset($_SESSION['vSession'][$this->name])) {
|
||||
$_SESSION['vSession'][$this->name] = array();
|
||||
$_SESSION['vSession'][$this->name]['created'] = time();
|
||||
$_SESSION['vSession'][$this->name]['modified'] = time();
|
||||
} else {
|
||||
throw new Exception("session exists");
|
||||
}
|
||||
}
|
||||
public function exists() {
|
||||
global $_SESSION;
|
||||
return isset($_SESSION['vSession']) && isset($_SESSION['vSession'][$this->name]);
|
||||
}
|
||||
public function destroy() {
|
||||
global $_SESSION;
|
||||
unset($_SESSION['vSession'][$this->name]);
|
||||
}
|
||||
public function destroyAll() {
|
||||
global $_SESSION;
|
||||
unset($_SESSION['vSession']);
|
||||
}
|
||||
public function setAttribute($name, $value) {
|
||||
global $_SESSION;
|
||||
if (count($_SESSION['vSession']) > MAX_VSESSIONS)
|
||||
$this->deleteOldest();
|
||||
$_SESSION['vSession'][$this->name]['modified'] = time();
|
||||
$_SESSION['vSession'][$this->name][$name] = $value;
|
||||
}
|
||||
public function getAttribute($name) {
|
||||
global $_SESSION;
|
||||
return $_SESSION['vSession'][$this->name][$name];
|
||||
}
|
||||
public function attributeIsset($name) {
|
||||
global $_SESSION;
|
||||
return isset($_SESSION['vSession'][$this->name][$name]);
|
||||
}
|
||||
private function deleteOldest() {
|
||||
global $_SESSION;
|
||||
$oldestKey = false;
|
||||
$last = time() + 1;
|
||||
foreach ($_SESSION['vSession'] as $key => $value) {
|
||||
if ($value['modified'] < $last) {
|
||||
$last = $value['modified'];
|
||||
$oldestKey = $key;
|
||||
}
|
||||
}
|
||||
if (!$oldestKey)
|
||||
return;
|
||||
unset($_SESSION['vSession'][$oldestKey]);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in a new issue