72 lines
1.5 KiB
PHP
Executable File
72 lines
1.5 KiB
PHP
Executable File
<?php
|
|
|
|
class Session {
|
|
|
|
public static $cache = null;
|
|
public static $id = null;
|
|
|
|
public static function getID() {
|
|
|
|
|
|
if (array_key_exists("SESSION_ID", $_COOKIE)) {
|
|
Session::$id = $_COOKIE["SESSION_ID"];
|
|
}
|
|
|
|
if (Session::$id) return Session::$id;
|
|
|
|
Session::$id = uniqid("decpdf", true);
|
|
|
|
setcookie("SESSION_ID", Session::$id, time() + 60*60*24*30);
|
|
return Session::$id;
|
|
}
|
|
|
|
public static function init($host, $port, $weight = 50) {
|
|
if (Session::$cache == null) {
|
|
Session::$cache = new Memcached();
|
|
}
|
|
Session::$cache->addServer($host, $port, $weight);
|
|
}
|
|
|
|
public static function get(string $key) : mixed {
|
|
$sid = Session::getID();
|
|
|
|
$key = "Session::" . $sid . "::" . $key;
|
|
|
|
$t = Session::$cache->get($key);
|
|
if ($t) {
|
|
return unserialize($t);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function set(string $key, mixed $value) : void {
|
|
$sid = Session::getID();
|
|
$fkey = "Session::" . $sid . "::" . $key;
|
|
Session::$cache->set($fkey, serialize($value));//, 60*60*24*30);
|
|
}
|
|
|
|
public static function unset(string $key) : void {
|
|
$sid = Session::getID();
|
|
$fkey = "Session::" . $sid . "::" . $key;
|
|
Session::$cache->delete($fkey);
|
|
}
|
|
|
|
public static function all_data() {
|
|
$sid = Session::getID();
|
|
$fkey = "Session::" . $sid . "::";
|
|
$keys = Session::$cache->getAllKeys();
|
|
$data = [];
|
|
foreach ($keys as $k) {
|
|
if (str_starts_with($k, $fkey)) {
|
|
$d = Session::get($k);
|
|
if (!$d) {
|
|
$d = [];
|
|
}
|
|
$out[$k] = $data;
|
|
}
|
|
}
|
|
return $out;
|
|
}
|
|
}
|