-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSession.php
More file actions
196 lines (161 loc) · 3.98 KB
/
Copy pathSession.php
File metadata and controls
196 lines (161 loc) · 3.98 KB
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
189
190
191
192
193
194
195
196
<?php namespace Mirage;
/**
* Created by PhpStorm.
* User: Dell
* Date: 18.05.2016
* Time: 12:55
*/
class Session extends \SessionHandler
{
protected $name, $cookie;
private $started = false;
public function __construct($name = 'MY_SESSION', $cookie = [])
{
$this->name = $name;
$this->cookie = $cookie;
$this->cookie += [
'lifetime' => ini_get('session.cookie_lifetime'),
'path' => ini_get('session.cookie_path'),
'domain' => ini_get('session.cookie_domain'),
'secure' => isset($_SERVER['HTTPS']),
'httponly' => true
];
$this->setup();
if (ini_get('session.auto_start')) {
$this->start();
}
}
protected function setup()
{
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
session_name($this->name);
session_set_cookie_params(
$this->cookie['lifetime'], $this->cookie['path'],
$this->cookie['domain'], $this->cookie['secure'],
$this->cookie['httponly']
);
#$this->isFingerprint();
#$this->isExpired();
}
public function start($force_create = false)
{
if (isset($_COOKIE[session_name()]) || $force_create) {
if (session_id() === '') {
if (session_start()) {
$this->started = true;
return true;
//return mt_rand(0, 4) === 0 ? $this->regenerate(true) : true; // 1/5
}
}
}
return false;
}
public function get($key, $default = false)
{
$this->started || $this->start();
if ( !is_string($key) ) {
throw new \Exception('Session key must be string value');
}
return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
}
public function pull($key, $default = false)
{
$value = $this->get($key, $default);
$this->forget($key);
return $value;
}
public function all()
{
$this->started || $this->start();
return $_SESSION;
}
public function has($key)
{
$this->started || $this->start();
if ( !is_string($key) ) {
throw new \Exception('Session key must be string value');
}
return isset($_SESSION[$key]);
}
public function set($key, $value = NULL)
{
$this->started || $this->start(true);
if ( !is_string($key) ) {
throw new \Exception('Session key must be string value');
}
$_SESSION[$key] = $value;
$this->timestamp();
}
public function forget($key)
{
if(isset($_SESSION[$key])) {
unset($_SESSION[$key]);
$this->timestamp();
}
}
public function flush()
{
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
session_destroy();
}
public function regenerate($delete_old_session = false)
{
return session_regenerate_id($delete_old_session);
}
public function flash($key)
{
$this->started || $this->start();
if(isset($_SESSION['_flashBag'][$key])) {
$value = $_SESSION['_flashBag'][$key];
unset($_SESSION['_flashBag'][$key]);
return $value;
}
return false;
}
public function setFlash($key, $value)
{
$this->started || $this->start(true);
$_SESSION['_flashBag'][$key] = $value;
}
public function isExpired($ttl = 30)
{
$this->started || $this->start();
$activity = isset($_SESSION['_last_activity'])
? $_SESSION['_last_activity']
: false;
if ($activity !== false && time() - $activity > $ttl * 60) {
return true;
}
$this->set('_last_activity', time());
return false;
}
public function isFingerprint()
{
$this->started || $this->start();
$hash = md5(
(isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '') .
(ip2long(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '') & ip2long('255.255.0.0'))
);
if (isset($_SESSION['_fingerprint'])) {
return $_SESSION['_fingerprint'] === $hash;
}
$this->set('_fingerprint', $hash);
return true;
}
public function isValid($ttl = 30)
{
return ! $this->isExpired($ttl) && $this->isFingerprint();
}
public function timestamp()
{
return $this->started ? $_SESSION['_lli'] = time() : false;
}
}