* @copyright 2013-2015 Jan Altensen (Stricted)
*/
class SessionHandler {
+ /**
+ * session id
+ *
+ * @var integer
+ */
private $sessionID = null;
+ /**
+ * session data
+ *
+ * @var array
+ */
private $sessionData = array();
+ /**
+ * initial session system
+ */
public function __construct () {
- $this->init();
- }
-
- public function init() {
if ($this->sessionID === null) {
$this->sessionID = session_id();
}
- // load session data from database and check if the data is expired
- if (!$this->exists()) {
- $sql = "INSERT INTO dns_session (id, sessionID, expire, sessionData) VALUES (NULL, ?, ?, ?)";
- DNS::getDB()->query($sql, array($this->sessionID, time() + 3600 * 24, ''));
- }
+ /* delete expired sessions */
+ $sql = "DELETE FROM dns_session WHERE expire < ?";
+ DNS::getDB()->query($sql, array(time()));
/* load data from database */
$sql ="SELECT * FROM dns_session where sessionID = ?";
$res = DNS::getDB()->query($sql, array($this->sessionID));
$data = DNS::getDB()->fetch_array($res);
- if (isset($data['sessionData']) && !empty($data['sessionData'])) {
- $this->sessionData = json_decode($data['sessionData'], true);
- }
- }
-
- private function exists() {
- $sql = "SELECT * FROM dns_session where sessionID = ?";
- $res = DNS::getDB()->query($sql, array($this->sessionID));
- $data = DNS::getDB()->fetch_array($res);
if (isset($data['sessionID']) && !empty($data['sessionID'])) {
- if ($data['expire'] < time()) {
- $this->destroy();
- return false;
+ if (isset($data['sessionData']) && !empty($data['sessionData'])) {
+ $this->sessionData = json_decode($data['sessionData'], true);
}
-
- return true;
}
-
- return false;
+ else {
+ $sql = "INSERT INTO dns_session (id, sessionID, expire, sessionData) VALUES (NULL, ?, ?, ?)";
+ DNS::getDB()->query($sql, array($this->sessionID, time() + 3600 * 24, ''));
+ }
}
/**
return $this->getVar($key);
}
+ /**
+ * Provides access to session data.
+ *
+ * @param string $key
+ * @return mixed
+ */
public function getVar($key) {
if (isset($this->sessionData[$key])) {
return $this->sessionData[$key];
public function register($key, $value) {
$this->sessionData[$key] = $value;
-
$data = json_encode($this->sessionData);
$sql = "UPDATE dns_session SET sessionData = ?, expire = ? WHERE sessionID = ?";
DNS::getDB()->query($sql, array($data, time() + 3600 * 24, $this->sessionID));
}
+ /**
+ * Registers a session variable.
+ *
+ * @param string $key
+ * @param string $value
+ */
public function __set($key, $value) {
$this->register($key, $value);
}
+ /**
+ * destroy the session
+ */
public function destroy() {
$this->sessionData = array();
DNS::getDB()->query($sql, array($this->sessionID));
}
+ /**
+ * Registers a session variable.
+ *
+ * @param string $key
+ * @param string $value
+ */
public function update($key, $value) {
$this->register($key, $value);
}