Skip to content

Instantly share code, notes, and snippets.

@lajosbencz
Created January 17, 2018 09:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lajosbencz/eb6869b6987e6a3eed70a1aab3f5c6fc to your computer and use it in GitHub Desktop.
Save lajosbencz/eb6869b6987e6a3eed70a1aab3f5c6fc to your computer and use it in GitHub Desktop.
use Phalcon\Session\AdapterInterface;
use Phalcon\Cache\Backend\Redis as RedisCache;
use Phalcon\Cache\Frontend\None as FrontendNone;
class Redis implements \SessionHandlerInterface, AdapterInterface
{
const SESSION_ACTIVE = 2;
const SESSION_NONE = 1;
const SESSION_DISABLED = 0;
protected $_uniqueId;
protected $_started = false;
protected $_options;
protected $_redis = null;
protected $_lifetime = 8600;
public function __construct($options = null)
{
if (!isset($options["host"])) {
$options["host"] = "127.0.0.1";
}
if (!isset($options["port"])) {
$options["port"] = 6379;
}
if (!isset($options["persistent"])) {
$options["persistent"] = false;
}
if (array_key_exists("lifetime", $options)) {
$this->_lifetime = $options['lifetime'];
}
$this->_redis = new RedisCache(new FrontendNone(), $options);
session_set_save_handler(
[$this, "open"],
[$this, "close"],
[$this, "read"],
[$this, "write"],
[$this, "destroy"],
[$this, "gc"]
);
if (is_array($options)) {
$this->setOptions($options);
}
}
public function getRedis()
{
return $this->_redis;
}
public function getLifetime()
{
return $this->_lifetime;
}
public function start()
{
if (!headers_sent()) {
if (!$this->_started && $this->status() !== self::SESSION_ACTIVE) {
session_start();
$this->_started = true;
return true;
}
}
return false;
}
public function setOptions(array $options=[])
{
if (array_key_exists('uniqueId', $options)) {
$this->_uniqueId = $options['uniqueId'];
}
$this->_options = $options;
}
public function getOptions()
{
return $this->_options;
}
/**
* Set session name
*/
public function setName($name)
{
session_name($name);
}
public function getName()
{
return session_name();
}
public function regenerateId($deleteOldSession = true)
{
session_regenerate_id($deleteOldSession);
return $this;
}
public function get($index, $defaultValue = null, $remove = false)
{
$uniqueId = $this->_uniqueId;
if (!empty($uniqueId)) {
$key = $uniqueId . "#" . $index;
} else {
$key = $index;
}
if (array_key_exists($key, $_SESSION)) {
$value = $_SESSION[$key];
if ($remove) {
unset($_SESSION[$key]);
}
return $value;
}
return $defaultValue;
}
public function set($index, $value)
{
$uniqueId = $this->_uniqueId;
if (!empty($uniqueId)) {
$_SESSION[$uniqueId . "#" . $index] = $value;
return;
}
$_SESSION[$index] = $value;
}
public function has($index)
{
$uniqueId = $this->_uniqueId;
if (!empty($uniqueId)) {
return isset($_SESSION[$uniqueId . "#" . $index]);
}
return isset($_SESSION[$index]);
}
public function remove($index)
{
$uniqueId = $this->_uniqueId;
if (!empty($uniqueId)) {
unset($_SESSION[$uniqueId . "#" . $index]);
return;
}
unset($_SESSION[$index]);
}
public function getId()
{
return session_id();
}
public function setId($id)
{
session_id($id);
}
public function isStarted()
{
return $this->_started;
}
/*
public function destroy($removeData = false)
{
if($removeData) {
$this->removeSessionData();
}
$this->_started = false;
return session_destroy();
}
*/
public function status()
{
$status = session_status();
switch ($status) {
case PHP_SESSION_DISABLED:
return self::SESSION_DISABLED;
case PHP_SESSION_ACTIVE:
return self::SESSION_ACTIVE;
}
return self::SESSION_NONE;
}
public function __get($index)
{
return $this->get($index);
}
public function __set($index, $value)
{
return $this->set($index, $value);
}
public function __isset($index)
{
return $this->has($index);
}
public function __unset($index)
{
$this->remove($index);
}
public function __destruct()
{
if ($this->_started) {
session_write_close();
$this->_started = false;
}
}
protected function removeSessionData()
{
$uniqueId = $this->_uniqueId;
if (empty($_SESSION)) {
return;
}
if (!empty($uniqueId)) {
foreach ($_SESSION as $key => $_) {
if (strpos($key, $uniqueId . '#') === 0) {
unset($_SESSION[$key]);
}
}
} else {
$_SESSION = [];
}
}
public function open($save_path, $name)
{
return true;
}
public function close()
{
return true;
}
public function read($sessionId)
{
return (string)$this->_redis->get($sessionId, $this->_lifetime);
}
public function write($sessionId, $data)
{
return $this->_redis->save($sessionId, $data, $this->_lifetime);
}
public function destroy($sessionId = null)
{
if ($sessionId === null) {
$id = $this->getId();
} else {
$id = $sessionId;
}
$this->removeSessionData();
return $this->_redis->exists($id) ? $this->_redis->delete($id) : true;
}
public function gc($maxlifetime = null)
{
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment