2016-09-19 06:40:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Web;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
class DbSessionHandler implements \SessionHandlerInterface {
|
2016-09-19 06:40:18 +00:00
|
|
|
private $db;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function __construct($db) {
|
2016-09-19 06:40:18 +00:00
|
|
|
$this->db = $db;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function open($savePath, $name) {
|
2016-09-19 06:40:18 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function close() {
|
2016-09-19 06:40:18 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function read($sessionId) {
|
|
|
|
$sessionData = $this->db->getValue(
|
2017-11-29 10:01:48 +00:00
|
|
|
'SELECT data FROM userSession WHERE ssid = #', [$sessionId]);
|
2018-05-23 10:14:20 +00:00
|
|
|
return isset($sessionData) ? $sessionData : '';
|
2016-09-19 06:40:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function write($sessionId, $sessionData) {
|
|
|
|
$this->db->query(
|
2016-10-15 18:58:30 +00:00
|
|
|
'INSERT INTO userSession SET
|
|
|
|
ssid = #, data = #, lastUpdate = NOW()
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
data = VALUES(data), lastUpdate = VALUES(lastUpdate)',
|
2016-09-19 06:40:18 +00:00
|
|
|
[$sessionId, $sessionData]);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2016-10-17 17:01:23 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function destroy($sessionId) {
|
|
|
|
$this->db->query('DELETE FROM userSession WHERE ssid = #', [$sessionId]);
|
2016-10-17 17:01:23 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function gc($maxLifeTime) {
|
|
|
|
$this->db->query('DELETE FROM userSession
|
2016-10-17 17:01:23 +00:00
|
|
|
WHERE lastUpdate < TIMESTAMPADD(SECOND, -#, NOW())',
|
|
|
|
[$maxLifeTime]
|
|
|
|
);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2016-09-19 06:40:18 +00:00
|
|
|
}
|