Elgg  Version 3.0
DatabaseSessionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Http;
4 
13 class DatabaseSessionHandler implements \SessionHandlerInterface {
14 
18  protected $db;
19 
25  public function __construct(\Elgg\Database $db) {
26  $this->db = $db;
27  }
28 
32  public function open($save_path, $name) {
33  return true;
34  }
35 
39  public function read($session_id) {
40 
41  $query = "SELECT *
42  FROM {$this->db->prefix}users_sessions
43  WHERE session = :session_id";
44  $params = [
45  ':session_id' => $session_id,
46  ];
47 
48  $result = $this->db->getDataRow($query, null, $params);
49  if (!empty($result)) {
50  return (string) $result->data;
51  }
52 
53  return '';
54  }
55 
59  public function write($session_id, $session_data) {
60 
61  if (elgg_get_config('_disable_session_save')) {
62  return true;
63  }
64 
65  $query = "INSERT INTO {$this->db->prefix}users_sessions
66  (session, ts, data) VALUES
67  (:session_id, :time, :data)
68  ON DUPLICATE KEY UPDATE ts = VALUES(ts), data = VALUES(data)";
69  $params = [
70  ':session_id' => $session_id,
71  ':time' => time(),
72  ':data' => $session_data,
73  ];
74 
75  if ($this->db->insertData($query, $params) !== false) {
76  return true;
77  }
78 
79  return false;
80  }
81 
85  public function close() {
86  return true;
87  }
88 
92  public function destroy($session_id) {
93 
94  $query = "DELETE FROM {$this->db->prefix}users_sessions
95  WHERE session = :session_id";
96  $params = [
97  ':session_id' => $session_id,
98  ];
99 
100  return (bool) $this->db->deleteData($query, $params);
101  }
102 
106  public function gc($max_lifetime) {
107 
108  $query = "DELETE FROM {$this->db->prefix}users_sessions
109  WHERE ts < :life";
110  $params = [
111  ':life' => (time() - $max_lifetime),
112  ];
113 
114  return (bool) $this->db->deleteData($query, $params);
115  }
116 }
$query
Definition: groups.php:8
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$params
Saves global plugin settings.
Definition: save.php:13
The Elgg database.
Definition: Database.php:21
Configuration exception.
__construct(\Elgg\Database $db)
Constructor.
write($session_id, $session_data)
elgg_get_config($name, $default=null)
Get an Elgg configuration value.