Elgg  Version 2.3
DatabaseSessionHandler.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Http;
3 
12 class DatabaseSessionHandler implements \SessionHandlerInterface {
13 
15  protected $db;
16 
22  public function __construct(\Elgg\Database $db) {
23  $this->db = $db;
24  }
25 
29  public function open($save_path, $name) {
30  return true;
31  }
32 
36  public function read($session_id) {
37 
38  $id = sanitize_string($session_id);
39  $query = "SELECT * FROM {$this->db->prefix}users_sessions WHERE session='$id'";
40  $result = $this->db->getDataRow($query);
41  if ($result) {
42  return (string) $result->data;
43  } else {
44  return false;
45  }
46  }
47 
51  public function write($session_id, $session_data) {
52  $id = sanitize_string($session_id);
53  $time = time();
54  $sess_data_sanitised = sanitize_string($session_data);
55 
56  $query = "INSERT INTO {$this->db->prefix}users_sessions
57  (session, ts, data) VALUES
58  ('$id', '$time', '$sess_data_sanitised')
59  ON DUPLICATE KEY UPDATE ts = '$time', data = '$sess_data_sanitised'";
60 
61  if ($this->db->insertData($query) !== false) {
62  return true;
63  } else {
64  return false;
65  }
66  }
67 
71  public function close() {
72  return true;
73  }
74 
78  public function destroy($session_id) {
79 
80  $id = sanitize_string($session_id);
81  $query = "DELETE FROM {$this->db->prefix}users_sessions WHERE session='$id'";
82  return (bool) $this->db->deleteData($query);
83  }
84 
88  public function gc($max_lifetime) {
89 
90  $life = time() - $max_lifetime;
91  $query = "DELETE FROM {$this->db->prefix}users_sessions WHERE ts < '$life'";
92  return (bool) $this->db->deleteData($query);
93  }
94 }
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
sanitize_string($string)
Sanitizes a string for use in a query.
Definition: database.php:153
Save menu items.
__construct(\Elgg\Database $db)
Constructor.
elgg subtext time
write($session_id, $session_data)
if(!$collection_name) $id
Definition: add.php:17