-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession.php
188 lines (172 loc) · 3.66 KB
/
Session.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* This file is part of the Zest Framework.
*
* @author Muhammad Umer Farooq (Malik) <[email protected]>
*
* @link https://github.com/zestframework/Zest_Framework
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @since 1.0.0
*
* @license MIT
*/
namespace Zest\Session;
class Session
{
/**
* __Construct.
*
* @since 1.0.0
*
* @return void
*/
public function __construct()
{
static::sessionPath();
static::start();
}
/**
* Start the session if not already start.
*
* @since 1.0.0
*
* @return void
*/
public static function start()
{
(session_status() === PHP_SESSION_NONE) ? session_start() : null;
}
/**
* Change session path.
*
* @since 2.0.0
*
* @return void
*/
public static function sessionPath()
{
if (\defined('__ZEST__ROOT__')) {
ini_set('session.save_path', session_path());
}
}
/**
* Check if session is already set with specific name.
*
* @param (string) $name name of session e.g users
*
* @since 1.0.0
*
* @return bool
*/
public static function has($name)
{
return (isset($_SESSION[$name])) ? true : false;
}
/**
* Get the session value by providing session name.
*
* @param (string) $name name of session e.g users
* @param (mixed) $default default value if sesion is not exists
*
* @since 1.0.0
*
* @return string
*/
public static function get($name, $default = null)
{
return (self::has($name)) ? $_SESSION[$name] : $default;
}
/**
* Set/store value in session.
*
* @param (string) $name name of session e.g users
* @param (string) $value value store in session e.g user token
*
* @since 1.0.0
*
* @return string
*/
public static function set($name, $value)
{
return (self::has($name) !== true) ? $_SESSION[$name] = $value : false;
}
/**
* Set/store multiple values in session.
*
* @param (array) $values keys and values
*
* @since 3.0.0
*
* @return object
*/
public function setMultiple($values)
{
foreach ($values as $value) {
self::set($value['key'], $value['value'], $ttl);
}
return self;
}
/**
* Get multiple values in session.
*
* @param (array) $keys keys
* @param (mixed) $default default value if sesion is not exists
*
* @since 3.0.0
*
* @return array
*/
public function getMultiple($keys)
{
$value = [];
foreach ($keys as $key) {
$value[$key] = self::get($key);
}
return $value;
}
/**
* Delete/unset the session.
*
* @param (string) $name name of session e.g users
*
* @since 1.0.0
*
* @return mixed
*/
public static function delete(string $name)
{
if (self::has($name)) {
unset($_SESSION[$name]);
}
return false;
}
/**
* Delete multiple values in session.
*
* @param (array) $keys keys
*
* @since 3.0.0
*
* @return void
*/
public function deleteMultiple($keys)
{
foreach ($keys as $key) {
self::delete($key);
}
}
/**
* Destroy the session.
*
* @since 3.0.0
*
* @return void
*/
public static function destroy()
{
session_unset();
session_destroy();
}
}