1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Core\Dependency; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Session |
7
|
|
|
* Session class to take care of all the session details |
8
|
|
|
* |
9
|
|
|
* @package Core |
10
|
|
|
* |
11
|
|
|
* PHP version 7 |
12
|
|
|
* |
13
|
|
|
* this is our session handler, of course we access the session superglobal. |
14
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
15
|
|
|
*/ |
16
|
|
|
class Session |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Session constructor. it the session isn't started then we start it |
20
|
|
|
*/ |
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
if (session_status() === PHP_SESSION_NONE) { |
24
|
|
|
session_start(); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* get the session values |
30
|
|
|
* |
31
|
|
|
* @param $param string the name of the parameter to get |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function get($param) |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
return $_SESSION[$param] ?? null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Checks if a parameter is set in the session |
42
|
|
|
* @param $param |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function isParamSet($param): bool |
46
|
|
|
{ |
47
|
|
|
return isset($_SESSION[$param]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* set the session parameter to somthing |
52
|
|
|
* |
53
|
|
|
* @param $param string paramter to set |
54
|
|
|
* @param $info mixed value to set the session param to |
55
|
|
|
*/ |
56
|
|
|
public function set($param, $info): void |
57
|
|
|
{ |
58
|
|
|
$_SESSION[$param] = $info; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set the session parameter only if nothing has been set yet |
63
|
|
|
* @param $param string parameter to set |
64
|
|
|
* @param $info mixed the info to store |
65
|
|
|
*/ |
66
|
|
|
public function setOnce($param, $info): void |
67
|
|
|
{ |
68
|
|
|
if (!isset($_SESSION[$param])) { |
69
|
|
|
$_SESSION[$param] = $info; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* removes elements from the session |
75
|
|
|
* @param $param string parameter to remove |
76
|
|
|
*/ |
77
|
|
|
public function remove($param): void |
78
|
|
|
{ |
79
|
|
|
unset($_SESSION[$param]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Remove all the session variables. |
84
|
|
|
*/ |
85
|
|
|
public function unsetAll(): void |
86
|
|
|
{ |
87
|
|
|
session_unset(); //remove all session variables |
88
|
|
|
$this->regenerateSessionId(); //regenerate the ID |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* return the entire session superglobal. |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function getAllSessionVars() |
96
|
|
|
{ |
97
|
|
|
return $_SESSION; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Unsets all the data the destroys the session |
102
|
|
|
*/ |
103
|
|
|
public function destroySession():void |
104
|
|
|
{ |
105
|
|
|
$this->unsetAll(); |
106
|
|
|
session_destroy(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* regenerate the session ID |
111
|
|
|
* This avoids session ghosting |
112
|
|
|
*/ |
113
|
|
|
public function regenerateSessionId():void |
114
|
|
|
{ |
115
|
|
|
session_regenerate_id(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |