Skip to content

Instantly share code, notes, and snippets.

@JeanSebTr
Created December 6, 2011 00:24
Show Gist options
  • Save JeanSebTr/1436075 to your computer and use it in GitHub Desktop.
Save JeanSebTr/1436075 to your computer and use it in GitHub Desktop.
Exemple de class pour gérer les Session sans le mécanisme de session PHP.
<?php
/**
* Author : Jean-Sébastien Tremblay <jean-seb@total-itech.com>
* Description : Class for scalable, stateless and client-side sessions
*/
/* Usage :
generate IV :
// do this only on time and save in config
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$conf_iv = base64_encode($iv);
config :
define('SESSION_NAME', 'myWebsiteCookie');
define('CONFIG_KEY', 'text clef encryption');
define('CONFIG_IV', ***generated iv***);
start session :
$sess = new Session(CONFIG_KEY, CONFIG_IV);
$sess->read(SESSION_NAME);
use :
$sess->myVar = 'foo bar';
echo $sess->myVar;
close session :
// important before sending any data to client
$sess->writeIfChange(SESSION_NAME);
*/
class Session
{
private $mod = null;
private $key;
private $iv;
private $data = array('expire'=>0, 'data'=>array());
private $change = false;
private $expire = 0;
private $path = '/';
public function __construct($key, $iv)
{
$this->key = $key;
$this->iv = base64_decode($iv);
}
public function config($expire, $path = '/')
{
$this->expire = $expire;
$this->path = $path;
}
public function read($name)
{
if(!array_key_exists($name, $_COOKIE) || empty($_COOKIE[$name]))
return false;
$encSess = base64_decode($_COOKIE[$name], true);
if($encSess === false)
return false;
if($this->mod === null)
$this->open();
mcrypt_generic_init($this->mod, $this->key, $this->iv);
$data = mdecrypt_generic($this->mod, $encSess);
mcrypt_generic_deinit($this->mod);
$len = intval(substr($data, 0, 4));
$this->data = json_decode(substr($data, 4, $len), true);
if($this->data['expire'] == 0 || $this->data['expire'] > time())
{
return true;
}
$this->data['data'] = array();
return false;
}
public function write($name)
{
if($this->mod === null)
$this->open();
$t = time();
if($this->data['expire'] < time())
$this->data['expire'] = ($this->expire == 0)?0:$t+$this->expire;
$data = json_encode($this->data);
mcrypt_generic_init($this->mod, $this->key, $this->iv);
$encSess = mcrypt_generic($this->mod, str_pad(strlen($data), 4, '0', STR_PAD_LEFT).$data);
mcrypt_generic_deinit($this->mod);
$sess = base64_encode($encSess);
setcookie($name, $sess, $t+(365*24*60*60), $this->path);
}
public function writeIfChange($name)
{
if($this->change)
return $this->write ($name);
return false;
}
private function open()
{
$this->mod = mcrypt_module_open('tripledes', '', 'ecb', '');
}
public function __destruct()
{
if($this->mod !== null)
mcrypt_module_close($this->mod);
}
public function __get($name)
{
if(array_key_exists($name, $this->data['data']))
return $this->data['data'][$name];
return null;
}
public function __set($name, $value)
{
$this->change = true;
$this->data['data'][$name] = $value;
}
public function destroy()
{
$this->data = array(
'data'=> array(),
'expire'=> 0
);
$this->data['expire'] = ($this->expire == 0)?0:time()+$this->expire;
$this->change = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment