1 <?php
2
3 namespace Alo\Session;
4
5 use Alo;
6 use Alo\Db\MySQL;
7
8 if (!defined('GEN_START')) {
9 http_response_code(404);
10 die();
11 }
12
13 14 15 16 17 18 19
20 class SQLSession extends AbstractSession {
21
22 23 24 25 26
27 protected $db;
28
29 30 31 32 33
34 function __construct() {
35 if (!Alo::$db) {
36 Alo::$db = new MySQL();
37 }
38
39 $this->db = &Alo::$db;
40 parent::__construct();
41 \Log::debug('Initialised MySQL session');
42 }
43
44 protected function write() {
45 $this->db->prepQuery('REPLACE INTO `'
46 . ALO_SESSION_TABLE_NAME . '`(`id`,`data`,`access`) VALUES('
47 . '?,?,CURRENT_TIMESTAMP)', [
48 $this->id,
49 json_encode($this->data)
50 ], [MySQL::V_CACHE => false]);
51
52 \Log::debug('Saved session data');
53
54 return $this;
55 }
56
57 protected function fetch() {
58 $sql = $this->db->prepQuery('SELECT `data` '
59 . 'FROM `' . ALO_SESSION_TABLE_NAME . '` '
60 . 'WHERE `id`=? '
61 . 'LIMIT 1', [$this->id], [
62 MySQL::V_CACHE => false
63 ]);
64
65 if (!empty($sql)) {
66 $this->data = json_decode($sql[0]['data'], true);
67 }
68
69 \Log::debug('Saved session data');
70
71 return $this;
72 }
73
74 function terminate() {
75 $this->db->prepQuery('DELETE FROM `' . ALO_SESSION_TABLE_NAME . '` '
76 . 'WHERE `id`=? '
77 . 'LIMIT 1', [$this->id], [MySQL::V_CACHE => false]);
78
79 return parent::terminate();
80 }
81
82 }