Skip to content

Instantly share code, notes, and snippets.

@chazmead
Created February 27, 2014 14:20
Show Gist options
  • Save chazmead/1688becbcf11f897e962 to your computer and use it in GitHub Desktop.
Save chazmead/1688becbcf11f897e962 to your computer and use it in GitHub Desktop.
/** * NEW SESSION MODEL TO BE USED INSTEAD OF CI SESSION LIBRARY * Main reasons are, CI Sessions are stored in DB or cookie.. personally I think PHP Session are much more efficient and secure than this. * Also this drastically simplifies using sessions.. so you can literally just assign a property to this class, and it saves it in the session.. …
<?php if (!defined('BASEPATH')) exit('No Direct Script Access Allowed');
/**
* NEW SESSION MODEL TO BE USED INSTEAD OF CI SESSION LIBRARY
* Main reasons are, CI Sessions are stored in DB or cookie.. personally I think PHP Session are much more efficient and secure than this.
* Also this drastically simplifies using sessions.. so you can literally just assign a property to this class, and it saves it in the session.. boom done..
* then that assigned property will be consistant for the user.
* i.e. from CI Controller / Model: $this->Session->anyNameString = 'Some data'
*
* Developer chazmead89@gmail.com
*/
$WC_SESSION_LOCK = False;
class Session
{
private $WC_SESSION_OPENED;
private $WC_SESSION_DATA;
private $WC_FLASH_DATA_BUFF;
private $WC_FLASH_DATA_STORE;
private $IP_ADDRESS;
private $USER_AGENT;
/**
* Session C'tor
* Sets up a session object, and assigns itself into the session key
* Removes Flashdata buffer if present
* Assigns flashdata buffer if any set in store, removes flash data store.
* @return null
*/
function __construct() {
$CI = &get_instance();
$conf = $CI->config->item('session');
global $WC_SESSION_LOCK;
$WC_SESSION_LOCK = True;
session_start();
$_SESSION['WC_SESSION_UID'] = $conf->UID;
if(!key_exists($conf->UID, $_SESSION) || time() - $_SESSION[$conf->UID]->WC_SESSION_OPENED > $conf->sess_expiration)
$_SESSION[$conf->UID] = $this->_newSession();
$sessBuffer = $_SESSION[$conf->UID];
session_write_close();
$ip = $CI->input->ip_address();
$user_agent = substr($CI->input->user_agent(), 0, 120);
if(isset($conf->match_ip) && $conf->match_ip && $ip !== $sessBuffer->IP_ADDRESS)
$sessBuffer = $this->_newSession();
if(isset($conf->match_user_agent) && $conf->match_user_agent && $user_agent !== $sessBuffer->USER_AGENT)
$sessBuffer = $this->_newSession();
foreach(get_object_vars($sessBuffer) as $key => $value)
$this->$key = $value;
$this->IP_ADDRESS = $ip;
$this->USER_AGENT = $user_agent;
if($this->WC_FLASH_DATA_STORE !== null)
$this->WC_FLASH_DATA_STORE = null;
if($this->WC_FLASH_DATA_BUFF !== null) {
$this->WC_FLASH_DATA_STORE = $this->WC_FLASH_DATA_BUFF;
$this->WC_FLASH_DATA_BUFF = null;
}
$WC_SESSION_LOCK = False;
$this->_saveSession();
}
/**
* Returns a new WC Session object
* @return object Blank session object with time created
*/
private function _newSession() {
return (object)array(
'WC_SESSION_OPENED' => time(),
'WC_SESSION_DATA' => new stdClass,
'IP_ADDRESS' => null,
'USER_AGENT' => null
);
}
/**
* Save the session
* Implemented to use session_write_close() methods to unlock session when
* not being saved to.
* @return null
*/
private function _saveSession() {
global $WC_SESSION_LOCK;
if(!$WC_SESSION_LOCK) {
@session_start();
$UID = $_SESSION['WC_SESSION_UID'];
$_SESSION[$UID] = $this;
session_write_close();
}
}
/**
* OVERLOADED SETTER
* Just to check the value being set is a valid string
* @param string $n Key name
* @param mixed $v Any value to be set in session
* @return null
*/
function __set($n,$v) {
if(!$n) return False;
$this->WC_SESSION_DATA->$n = $v;
$this->_saveSession();
}
/**
* OVERLOADED FALL BACK GETTER
* Checks the flashdata buffer
* @return bool false
*/
function __get($n) {
if(property_exists($this->WC_SESSION_DATA, $n))
return $this->WC_SESSION_DATA->$n;
if($this->WC_FLASH_DATA_STORE !== null) {
if(isset($this->WC_FLASH_DATA_STORE->$n))
return $this->WC_FLASH_DATA_STORE->$n;
}
return False;
}
/**
* OVERLOADED ISSET
* Passes the isset command onto the Session Data
* @param string $n name of variable for checking.
* @return bool
*/
function __isset($n) {
return isset($this->WC_SESSION_DATA->$n);
}
/**
* OVERLOADED UNSET
* Passes the unset command onto the Session Data
* @param string $n name of variable to unset
* @return null
*/
function __unset($n) {
unset($this->WC_SESSION_DATA->$n);
$this->_saveSession();
}
/**
* Session Flash data
* Mostly for backward compatibility to CI_Session
* But also quite usefull, flash data active for only the next request.
* @param string $name set the name for the flashdata
* @param mixed $value Any value for the session
* @return bool
*/
function set_flashdata($name,$value) {
global $WC_SESSION_LOCK;
$WC_SESSION_LOCK = True;
if($this->WC_FLASH_DATA_BUFF === null)
$this->WC_FLASH_DATA_BUFF = new stdClass;
$this->WC_FLASH_DATA_BUFF->$name = $value;
$WC_SESSION_LOCK = False;
$this->_saveSession();
return True;
}
/**
* Get Session flash data
* Mostly for backward compatibility as the overridden getter checks the
* flash data buffer
* @param string $name Name of the session key
* @return mixed any stored value
*/
function flashdata($name) {
if($this->WC_FLASH_DATA_STORE === null || !isset($this->WC_FLASH_DATA_STORE->$name)) return False;
return $this->WC_FLASH_DATA_STORE->$name;
}
function keep_flashdata($key) {
$data = $this->flashdata($key);
return $this->set_flashdata($key,$data);
}
/**
* Extend the session time out
* Implemented by putting forward the current session opened time
*
* if time not set, defaults to the session expiration in the config
*
* @param int $time in seconds to extend by
* @return bool
*/
function extendSession($time=False) {
if ($time === False) {
$CI = &get_instance();
$conf = $CI->config->item('session');
$time = $conf->sess_expiration;
}
else if (!is_numeric($time))
return False;
else {
$maxTime = (int)(time() - $this->WC_SESSION_OPENED);
$time = min($maxTime,(int)$time);
$this->WC_SESSION_OPENED += (int)$time;
$this->_saveSession();
return True;
}
}
/**
* Mostly for backward compatibility
* Destroy the session
* @return bool
*/
function sess_destroy() {
session_start();
session_destroy();
return True;
}
/**
* A way to save a bulk of session variables in a single session write
* Stops the class from opening and closing the session over and over.
* @param $data mixed object or array of data to be added by key=>value
* @return null
*/
function setMultiple($data) {
if(is_object($data))
$data = array($data);
global $WC_SESSION_LOCK;
$WC_SESSION_LOCK = True;
foreach($data as $n => $v)
$this->$n = $v;
$WC_SESSION_LOCK = False;
$this->_saveSession();
}
/**
* BACKWARD COMPATIBILITY TO OLD CI SESSION
* User data function from old CI Session.
*/
function userdata($key) {
return $this->WC_SESSION_DATA->$key;
}
function set_userdata($key, $data) {
$this->$key = $data;
}
function all_userdata($asArray=True) {
if($asArray)
return (array)$this->WC_SESSION_DATA;
else
return $this->WC_SESSION_DATA;
}
function unset_userdata($key) {
unset($this->$key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment