-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionManager.php
96 lines (77 loc) · 1.88 KB
/
SessionManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace SessionManager;
class SessionManager {
private static $_sessionStarted = false;
public function __construct() {}
/**
* Initializes a new session with $sessionName and returns the current session name.
*/
public static function init($sessionName=null) {
if($sessionName) {
//switch to another session by its name
SessionManager::change($sessionName);
}
if(!SessionManager::$_sessionStarted) {
//initialize or resume a session
session_start();
SessionManager::$_sessionStarted = true;
}
return session_name();
}
/**
* Changes the current session name
*/
public static function change($sessionName) {
return session_name($sessionName);
}
/**
* Returns the ID of the current session
*/
public static function getID() {
return session_id();
}
/**
* Stores a value for this current session
*/
public static function store($key, $value) {
$_SESSION[$key] = $value;
}
/**
* Deletes a value on this current session
*/
public static function delete($key) {
$_SESSION[$key] = NULL;
}
/**
* Return True if value exists in the current session, false otherwise
*/
public static function exists($key) {
return isset($_SESSION[$key]);
}
/**
* Return the value corresponding to $key if it exists, NULL otherwise
*/
public static function get($key) {
if(SessionManager::exists($key)) {
return $_SESSION[$key];
}
return NULL;
}
/*
* Destroys the current session along with all data. Must be called only if a session exists
*/
public static function clear() {
$_SESSION = array();
if(isset($_COOKIE[session_name()])){
setcookie(session_name(),'', time()-48000,'/');
}
session_destroy();
}
/**
* Generates a String representation of the current data in the session
*/
public static function toString($sessionName=null) {
return '<pre>' . print_r($_SESSION, true) . '</pre>';
}
}
?>