Skip to content

Commit d5634f5

Browse files
committed
address issue #3: avoid calling session_regenerate_id from inside the handler to make it compatible with PHP 7.1.2+
1 parent d7601a2 commit d5634f5

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/RedisSessionHandler.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class RedisSessionHandler extends \SessionHandler
2020
const MAX_WAIT_TIME = 128000;
2121

2222
/**
23+
* The Redis client.
24+
*
2325
* @var \Redis
2426
*/
2527
private $redis;
@@ -60,7 +62,7 @@ class RedisSessionHandler extends \SessionHandler
6062
*
6163
* @var string[]
6264
*/
63-
private $new_sessions;
65+
private $new_sessions = [];
6466

6567
/**
6668
* A collection of every session ID that is being locked by
@@ -69,7 +71,14 @@ class RedisSessionHandler extends \SessionHandler
6971
*
7072
* @var string[]
7173
*/
72-
private $open_sessions;
74+
private $open_sessions = [];
75+
76+
/**
77+
* The name of the session cookie.
78+
*
79+
* @var string
80+
*/
81+
private $cookieName = null;
7382

7483
public function __construct()
7584
{
@@ -80,15 +89,15 @@ public function __construct()
8089
$this->redis = new \Redis();
8190
$this->lock_ttl = intval(ini_get('max_execution_time'));
8291
$this->session_ttl = intval(ini_get('session.gc_maxlifetime'));
83-
$this->new_sessions = [];
84-
$this->open_sessions = [];
8592
}
8693

8794
/**
8895
* {@inheritdoc}
8996
*/
9097
public function open($save_path, $name)
9198
{
99+
$this->cookieName = $name;
100+
92101
list(
93102
$host, $port, $timeout, $prefix, $auth, $database
94103
) = SavePathParser::parse($save_path);
@@ -128,20 +137,18 @@ public function create_sid()
128137
public function read($session_id)
129138
{
130139
if ($this->mustRegenerate($session_id)) {
131-
// Regenerating the ID will call destroy(), close(), open(), create_sid() and read() in this order.
132-
// It will also signal the PHP internals to include the 'Set-Cookie' with the new ID in the HTTP response.
133-
session_regenerate_id(true);
140+
session_id($session_id = $this->create_sid());
134141

135-
return '';
142+
setcookie($this->cookieName, $session_id);
136143
}
137144

138145
$this->acquireLockOn($session_id);
139146

140-
if (false === $session_data = $this->redis->get($session_id)) {
141-
$session_data = '';
147+
if ($this->isNew($session_id)) {
148+
return '';
142149
}
143150

144-
return $session_data;
151+
return $this->redis->get($session_id);
145152
}
146153

147154
/**
@@ -227,7 +234,17 @@ private function releaseLocks()
227234
*/
228235
private function mustRegenerate($session_id)
229236
{
230-
return false === isset($this->new_sessions[$session_id])
237+
return false === $this->isNew($session_id)
231238
&& false === $this->redis->exists($session_id);
232239
}
240+
241+
/**
242+
* @param string $session_id
243+
*
244+
* @return bool
245+
*/
246+
private function isNew($session_id)
247+
{
248+
return isset($this->new_sessions[$session_id]);
249+
}
233250
}

0 commit comments

Comments
 (0)