Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------
Expand Down
77 changes: 76 additions & 1 deletion src/rcastera/Browser/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*
Expand Down