From b1db0be13d136ebc4e030b19c775b7cc54f48410 Mon Sep 17 00:00:00 2001 From: Radoslav Yordanov Date: Mon, 30 May 2016 14:53:24 +0300 Subject: [PATCH 1/2] added sql query for session table --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d9ff350..2e7c2f5 100755 --- a/README.md +++ b/README.md @@ -44,7 +44,20 @@ use rcastera\Browser\Session\Session; $session = new Session(); ``` - +### Store session in Database +----------------------------- +1. Execute this sql +CREATE TABLE IF NOT EXISTS `session` ( + `id` char(32) NOT NULL DEFAULT '', + `name` char(32) NOT NULL DEFAULT '', + `modified` int(11) DEFAULT NULL, + `lifetime` int(11) DEFAULT NULL, + `data` mediumblob, + PRIMARY KEY (`id`,`name`), + KEY `id` (`id`,`name`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +2. Just pass pdo object to session constructor and you have sessions in database ### Examples ----------------- From 91cf3ef4ba09a8f482b13c33f3b1738a86a67735 Mon Sep 17 00:00:00 2001 From: Radoslav Yordanov Date: Mon, 30 May 2016 14:54:06 +0300 Subject: [PATCH 2/2] Save session to database support --- src/rcastera/Browser/Session/Session.php | 77 +++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/src/rcastera/Browser/Session/Session.php b/src/rcastera/Browser/Session/Session.php index cbe38fa..12cd6cb 100755 --- a/src/rcastera/Browser/Session/Session.php +++ b/src/rcastera/Browser/Session/Session.php @@ -24,14 +24,32 @@ class Session { + protected $db; /** * Constructor. */ - public function __construct() + public function __construct($db=null) { + // The following prevents unexpected effects when using objects as save handlers. + if($db instanceof \PDO){ + $this->db = $db; + $this->setDatabaseHandler(); + } session_start(); } + protected function setDatabaseHandler() + { + session_set_save_handler( + array($this, "__open"), + array($this, "__close"), + array($this, "__read"), + array($this, "__write"), + array($this, "__destroy"), + array($this, "__clean")); + register_shutdown_function('session_write_close'); + } + /** * Destructor. */ @@ -40,6 +58,63 @@ public function __destruct() unset($this); } + public function __open() + { + $this->db->exec("set names utf8"); + } + + public function __close() + { + $this->db = null; + } + + public function __read($id) + { + $stmt = $this->db->prepare("SELECT * FROM session WHERE id=:id"); + $stmt->bindValue(":id", $id); + $stmt->execute(); + $res = $stmt->fetch(\PDO::FETCH_ASSOC); + return $res['data']; + } + + public function __write($id, $data) + { + $stmt = $this->db->prepare("REPLACE INTO session VALUES (:id, :name, :modified, :lifetime, :data)"); + $stmt->bindValue(":id", $id); + $stmt->bindValue(":name", $_SERVER['HTTP_HOST']); + $stmt->bindValue(":modified", time()); + $stmt->bindValue(":lifetime", 6000); + $stmt->bindValue(":data", $data); + $stmt->execute(); + } + + public function __clean($max) + { + $old = time() - $max; + $stmt = $this->db->prepare("DELETE FROM session WHERE modified < :old"); + $stmt->bindValue(":old", $old); + $stmt->execute(); + } + + public function __destroy($id) + { + $stmt = $this->db->prepare("DELETE FROM session WHERE id=:id"); + $stmt->bindValue(":id", $id); + $stmt->execute(); + } + + public function getOnline() + { + $stmt = $this->db->prepare(" + SELECT * + FROM `session` + WHERE modified + lifetime < NOW( )"); + $stmt->execute(); + $res = $stmt->fetchAll(); + return $res; + } + + /** * Register the session. *