Skip to content
Molchy edited this page Jul 8, 2012 · 37 revisions

NDB Session Class for CodeIgniter (Version 2.1)


Native Session makes good use of PHP’s native session handling abilities, but it does not allow the use of a database for session storage. Saving user session data into database is more secure on any type of hosting (Shared ... ). \ This library overwrites normal native sassion functions too save user data directly into database and gives us some extra functions over CI Session.

Overview


  • Is based on Native Session but has database functionality built in.
  • Compatible with CodeIgniter 1.7 +.
  • Drop-in replacement for CI’s Session library.
  • Config options and flash data are supported but not session encryption.
  • When using with a database, only the session_id is stored in a cookie. Any other data is stored in the database (Nativly would be stored to server /tmp ).
  • Tested IE6, IE7, IE8, IE9, Firefox 4, Chrome
  • PHP5+

Donations


Support Development

  • Support the developer and u may see more stuff coming
  • PayPal Support and Verified

Licence


GPL 2.0 and LGPL 2.1

Usage


Same usage as CI Session


Codeigniter Docs

Extras


**Write and Access session data from database: **

$_SESSION['Data'] = 'Value'; // Access, same as $this->session->userdata('Data');
$_SESSION['Data];  // Write, same as $this->session->set_userdata('Data', 'Value');
  • Get User Online Count:
$count = $this->session->users_online(300); // Returns all user which are or were active in last 5 min

Parsing session data:

// Enable us to access any session data and manage it, usefull for session management system
// We just past session data from database into session parse function and parse function will return session data in array style so we can access it

$arr_data = $this->session->parse($session_data);
$some_val = $arry_data['session_key'];

Install


  • Download Session Class and copy it to "appliaction/libraries/"
  • Insert new database table for Session storage
  • Autoload or load CI database Class and Session Class
  • Don't forget to setup database configuration
  • Have fun

Differences between NDB Session and CI Session


  • Using native PHP Session functions ( Smaller lib. same security )
  • No encryption of cookie as only Session ID is stored in cookie (Allready hashed)
  • Checking valid Session ID differently ( We dont save IP, Useragent and Activity in cookie)
  • Session garbage collector works differently ( Expired data, Useless data)
  • After session destroy we create new empty session so u can set new Session data after it
  • Some CI Session functions removed as not needet (Functions which PHP does it for us)
  • Extras for setting and accessing Session data( $_SESSION['data'] ...)

Required Database Structure


CREATE  TABLE IF NOT EXISTS `Sessions` (
 `sess_id` VARCHAR(40) NOT NULL ,
 `sess_ip` VARCHAR(16) NOT NULL ,
 `sess_user_agent` VARCHAR(120) NOT NULL ,
 `sess_date_activity` DATETIME NOT NULL ,
 `sess_date_expire` DATETIME NOT NULL ,
 `sess_date_expire_id` DATETIME NOT NULL ,
 `sess_data` TEXT NOT NULL ,
 PRIMARY KEY (`sess_id`) )
ENGINE = InnoDB

Example Configuration (/system/application/config/config.php)


$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 3600;  // 1 H
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'Sessions';
$config['sess_match_ip']        = TRUE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update']  = 600;  // 10 min

$config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';

Changelog


  • From 2.0 to 2.1 - Change database structure and Session Class

Help and Support


If any issues, mistakes or bugs report them on BitBucket ...


Copyright 2011 - 2012 by Denis Molan. All Rights Reserved

Clone this wiki locally