Skip to content

Instantly share code, notes, and snippets.

@apcomplete
Created November 28, 2011 20:12
Show Gist options
  • Save apcomplete/1401830 to your computer and use it in GitHub Desktop.
Save apcomplete/1401830 to your computer and use it in GitHub Desktop.
Native session library for CodeIgniter
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter Native Session Library
*
* @package Session
* @subpackage Libraries
* @category Session
* @author Topic Deisgn
*/
class MY_Session
{
protected $app_name = '';
protected $store = array();
// --------------------------------------------------------------------
/**
* Constructor
*
* @access public
* @param array config preferences
*
* @return void
**/
function __construct($config = array())
{
if ( ! isset($_SESSION))
{
session_start();
}
$this->initialize($config);
}
// --------------------------------------------------------------------
/**
* Initialize the configuration options
*
* @access private
* @param array config options
* @return void
*/
private function initialize($config)
{
foreach ($config as $key => $val)
{
if (method_exists($this, 'set_'.$key))
{
$this->{'set_'.$key}($val);
}
else if (isset($this->$key))
{
$this->$key = $val;
}
}
if(isset($_SESSION[$this->app_name]) )
{
$this->store = $_SESSION[$this->app_name];
if(! $this->is_expired())
{
return;
}
}
$this->create_session();
}
/**
* Create Session
*
* @access public
* @return void
*/
public function create_session()
{
$ci = get_instance();
$_SESSION[$this->app_name] = array(
'session_id' => md5(microtime()),
'expire_at' => time() + $ci->config->item('sess_expiration')
);
$this->store = $_SESSION[$this->app_name];
}
/**
* Check if session is expired
*
* @access public
* @return void
*/
public function is_expired()
{
if ( ! isset($this->store['expire_at']))
{
return TRUE;
}
return (time() > $this->store['expire_at']);
}
/**
* Destroy session
*
* @access public
*/
public function sess_destroy()
{
$this->create_session();
}
/**
* Get specific user data element
*
* @access public
* @param string element key
* @return object element value
*/
public function userdata($value)
{
if ($value = 'session_id')
{
return $this->store['session_id'];
}
if (isset($this->store[$value]))
{
return $this->store[$value];
}
else
{
return FALSE;
}
}
/**
* Set value for specific user data element
*
* @access public
* @param array list of data to be stored
* @param object value to be stored if only one element is passed
* @return void
*/
public function set_userdata($data = array(), $value = '')
{
if(is_string($data))
{
$data = array($data => $value);
}
foreach ($data as $key => $val)
{
$this->store[$key] = $val;
}
$_SESSION[$this->app_name] = $this->store;
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Session Config
| -------------------------------------------------------------------------
*/
/**
* Set namespacing for specific application in case
* user is using multiple applications run off of the same server.
*
* @var string
**/
$config['app_name'] = '';
@ssgalla
Copy link

ssgalla commented Apr 26, 2018

How can I integrate this into my CI application so I can keep my native php session going from my main application in my CI application.

@proj135dvx
Copy link

What is the difference between this and the default session library in codeigniter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment