aboutsummaryrefslogtreecommitdiffstats
path: root/public/system/library/session/db.php
diff options
context:
space:
mode:
Diffstat (limited to 'public/system/library/session/db.php')
-rw-r--r--public/system/library/session/db.php49
1 files changed, 49 insertions, 0 deletions
diff --git a/public/system/library/session/db.php b/public/system/library/session/db.php
new file mode 100644
index 0000000..99882c7
--- /dev/null
+++ b/public/system/library/session/db.php
@@ -0,0 +1,49 @@
+<?php
+/*
+CREATE TABLE IF NOT EXISTS `session` (
+ `session_id` varchar(32) NOT NULL,
+ `data` text NOT NULL,
+ `expire` datetime NOT NULL,
+ PRIMARY KEY (`session_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
+*/
+namespace Session;
+final class DB {
+ public $expire = '';
+
+ public function __construct($registry) {
+ $this->db = $registry->get('db');
+
+ $this->expire = ini_get('session.gc_maxlifetime');
+ }
+
+ public function read($session_id) {
+ $query = $this->db->query("SELECT `data` FROM `" . DB_PREFIX . "session` WHERE session_id = '" . $this->db->escape($session_id) . "' AND expire > " . (int)time());
+
+ if ($query->num_rows) {
+ return json_decode($query->row['data'], true);
+ } else {
+ return false;
+ }
+ }
+
+ public function write($session_id, $data) {
+ if ($session_id) {
+ $this->db->query("REPLACE INTO `" . DB_PREFIX . "session` SET session_id = '" . $this->db->escape($session_id) . "', `data` = '" . $this->db->escape(json_encode($data)) . "', expire = '" . $this->db->escape(date('Y-m-d H:i:s', time() + $this->expire)) . "'");
+ }
+
+ return true;
+ }
+
+ public function destroy($session_id) {
+ $this->db->query("DELETE FROM `" . DB_PREFIX . "session` WHERE session_id = '" . $this->db->escape($session_id) . "'");
+
+ return true;
+ }
+
+ public function gc($expire) {
+ $this->db->query("DELETE FROM `" . DB_PREFIX . "session` WHERE expire < " . ((int)time() + $expire));
+
+ return true;
+ }
+}