Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 17 revisions

Code Igniter bundles a session class, working with cookies and limited database support in version 1.6. Unfortunately, this class stores session userdata directly inside the cookie, even using the database.

[h2]Overview[/h2]

  • Based on a combination of Codeignitors Session.php in version 1.6 and DBSession.
  • Fully compatible with Codeignitors Session.php in version 1.54 and 1.6 and DBSession.
  • Designed as drop-in replacement for CI Session and/or DBSession.
  • Any config option like encryption and any functionallity like flash session variables are fully supported.
  • When using a database, only the session_id is stored in a cookie. Any other data is stored in the database.
  • When using without a database, all data is stored in a cookie.
  • Both modi work fully tansparent.

[h2]Download[/h2] File:NGSession.zip

[h2]Required database structure[/h2] Example Mysql: [code] CREATE TABLE ci_sessions ( session_id varchar(40) NOT NULL default '0', ip_address varchar(16) NOT NULL default '0', user_agent varchar(50) NOT NULL, last_activity int(10) unsigned NOT NULL default '0', session_data text, PRIMARY KEY (session_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; [/code] Note:

  • The table is similar to the orginal CI session table definition, execpt that it adds a field session_data to keep userdata and flash variables.
  • When using DBSession, the table is pretty much the same. So NGSession will not require any additional database config.
  • UTF8 is not necessary but recommanded.
  • Of cause, the database library must be loaded.

Example configuration (config.php)

=================================== In fact these are the orginal CI (version 1.6) configuration options. "$config['sess_use_database']" defines wether to use cookie or database mode. [code] $config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 7200; $config['sess_encrypt_cookie'] = FALSE; $config['sess_table_name'] = 'ci_sessions'; $config['sess_match_ip'] = TRUE; $config['sess_match_useragent'] = TRUE; $config['sess_use_database'] = TRUE; $config['sess_time_to_update'] = 300; [/code]

Usage

- Simply replace CI's session.php with this one. - Use this lib as if you would CI session.php.

Test Case

- Create a simple controller and use the session lib. - Set $config['sess_use_database'] = FALSE: All data should be stored in the cookie. - Set $config['sess_use_database'] = TRUE: All data should be stored in the database. The cookie should only contain the session_id.

Example controller: [code] class Main extends Controller {

function Main()
{
    parent::Controller();
    $this->load->library('view');
    // this starts a session if none exists
    $this->load->library('session');
}

[/code] Now the session data can be set/get like: [code] if (!$this->session->userdata('user_id')) {} [/code]

Methods

[code] set_userdata($newdata = array(), $newval = '') unset_userdata($newdata = array()) all_userdata()

set_flashdata($newdata = array(), $newval = '') keep_flashdata($key) flashdata($key) [/code] See the codeignitor documentation for more details.

Support

Pls visit the codeignitor forum

Category:Session

Category:Libraries::Session

Clone this wiki locally