Elgg  Version 1.11
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->getTablePrefix()}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 = "REPLACE INTO {$this->db->getTablePrefix()}users_sessions
57  (session, ts, data) VALUES
58  ('$id', '$time', '$sess_data_sanitised')";
59 
60  if ($this->db->insertData($query) !== false) {
61  return true;
62  } else {
63  return false;
64  }
65  }
66 
70  public function close() {
71  return true;
72  }
73 
77  public function destroy($session_id) {
78 
79  $id = sanitize_string($session_id);
80  $query = "DELETE FROM {$this->db->getTablePrefix()}users_sessions WHERE session='$id'";
81  return (bool) $this->db->deleteData($query);
82  }
83 
87  public function gc($max_lifetime) {
88 
89  $life = time() - $max_lifetime;
90  $query = "DELETE FROM {$this->db->getTablePrefix()}users_sessions WHERE ts < '$life'";
91  return (bool) $this->db->deleteData($query);
92  }
93 }
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
Save menu items.
__construct(\Elgg\Database $db)
Constructor.
write($session_id, $session_data)
if(!$collection_name) $id
Definition: add.php:17