X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;ds=sidebyside;f=lib%2FSessionHandler.php;fp=lib%2FSessionHandler.php;h=d3bc0080867bdf8e2cdfec42974fce14cbe4da22;hb=ae839ebaa4e1e0ae4cee28c16cc0bc7a0368f4af;hp=0000000000000000000000000000000000000000;hpb=ea0e53db8b5a264b6e231038815d2c350076d02a;p=readifood.git diff --git a/lib/SessionHandler.php b/lib/SessionHandler.php new file mode 100644 index 0000000..d3bc008 --- /dev/null +++ b/lib/SessionHandler.php @@ -0,0 +1,168 @@ + + * @link https://github.com/j2L4e/PHP-PDO-MySQL-Session-Handler + * + * + * Based on PHP-MySQL-Session-Handler (uses mysqli) + * + * @author Manuel Reinhard + * @link https://github.com/sprain/PHP-MySQL-Session-Handler + */ + +use PDO; +use SessionHandlerInterface; + +class SessionHandler implements SessionHandlerInterface +{ + /** + * a PDO connection resource + * @var resource + */ + protected $dbh; + + + /** + * the name of the DB table which handles the sessions + * @var string + */ + protected $dbTable; + + + /** + * Set db data if no connection is being injected + * @param string $dbHost + * @param string $dbUser + * @param string $dbPassword + * @param string $dbDatabase + * @param string $dbCharset optional, default 'utf8' + */ + public function setDbDetails($dbHost, $dbUser, $dbPassword, $dbDatabase, $dbCharset = 'utf8') { + + //create db connection + $this->dbh = new PDO("mysql:" . + "host={$dbHost};" . + "dbname={$dbDatabase};" . + "charset={$dbCharset}", + $dbUser, + $dbPassword, + array( + PDO::ATTR_EMULATE_PREPARES => false, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION //USE ERRMODE_SILENT FOR PRODUCTION! + ) + ); + }//function + + + /** + * Inject PDO from outside + * @param object $dbh expects PDO object + */ + public function setPDO($dbh) { + $this->dbh = $dbh; + } + + + /** + * Set MySQL table to work with + * @param string $dbTable + */ + public function setDbTable($dbTable) { + $this->dbTable = $dbTable; + } + + + /** + * Open the session + * @return bool + */ + public function open($save_path, $session_name) { + //delete old session handlers + $limit = time() - (3600 * 24); + $stmt = $this->dbh->prepare("DELETE FROM {$this->dbTable} WHERE timestamp < :limit"); + $ret = $stmt->execute(array(':limit' => $limit)); + + return $ret; + } + + /** + * Close the session + * @return bool + */ + public function close() { + $this->dbh = null; + } + + /** + * Read the session + * @param int session id + * @return string string of the sessoin + */ + public function read($id) { + $stmt = $this->dbh->prepare("SELECT * FROM {$this->dbTable} WHERE id=:id"); + $stmt->execute(array(':id' => $id)); + + $session = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($session) { + $ret = $session['data']; + } else { + $ret = false; + } + + return $ret; + } + + + /** + * Write the session + * @param int session id + * @param string data of the session + */ + public function write($id, $data) { + $stmt = $this->dbh->prepare("REPLACE INTO {$this->dbTable} (id,data,timestamp) VALUES (:id,:data,:timestamp)"); + $ret = $stmt->execute( + array(':id' => $id, + ':data' => $data, + 'timestamp' => time() + )); + + return $ret; + } + + /** + * Destroy the session + * @param int session id + * @return bool + */ + public function destroy($id) { + $stmt = $this->dbh->prepare("DELETE FROM {$this->dbTable} WHERE id=:id"); + $ret = $stmt->execute(array( + ':id' => $id + )); + + return $ret; + } + + + /** + * Garbage Collector + * @param int life time (sec.) + * @return bool + * @see session.gc_divisor 100 + * @see session.gc_maxlifetime 1440 + * @see session.gc_probability 1 + * @usage execution rate 1/100 + * (session.gc_probability/session.gc_divisor) + */ + public function gc($max) { + $stmt = $this->dbh->prepare("DELETE FROM {$this->dbTable} WHERE timestamp < :limit"); + $ret = $stmt->execute(array(':limit' => time() - intval($max))); + + return $ret; + } + +}//class