CI的session有点问题,很多时候需要挂载其他的扩展session,CI中国论坛网友推荐了kndb session,我大致用了一下,下面简单介绍下用法:
第一步:建立系统数据库,这里假设数据库名是test;
第二步:建立存储session的数据表sessions
第三步:修改CI文件夹/system/application/config/config.php
- $config['sess_cookie_name'] = 'CISESSION';
- $config['sess_expiration'] = 7200;
- $config['sess_encrypt_cookie'] = FALSE;
- $config['sess_table_name'] = 'sessions';
- $config['sess_match_ip'] = TRUE;
- $config['sess_match_useragent'] = TRUE;
- $config['sess_use_database'] = TRUE;
- $config['sess_time_to_update'] = 300;
第四步:在CI文件夹/system/application/libraries下建立文件Session.php,输入以下内容
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * Code Igniter
- *
- * An open source application development framework for PHP 4.3.2 or newer
- *
- * @package CodeIgniter
- * @author Elise Bosse
- * @copyright Copyright (c) 2008, E.Bosse
- * @license http://www.codeignitor.com/user_guide/license.html
- * @link http://www.codeigniter.com
- * @since Version 1.2
- * @filesource
- */
- // ------------------------------------------------------------------------
- /**
- * Session class using native PHP session features and hardened against session fixation.
- * Non-database part is based upon Dariusz Debowczyk's Native session library with some updates.
- * The DB part makes use of PHP's session_set_save_handler() functionality
- * This library is written for PHP 5 but it could be altered a bit to make it work in PHP4
- *
- * @package CodeIgniter
- * @subpackage Libraries
- * @category Sessions
- * @author Elise Bosse
- * @link http://www.codeigniter.com/user_guide/libraries/sessions.html
- * @class link http://codeigniter.com/wiki/KNDB_Session/
- */
- /**
- * If using a database, create the following database table and make sure config file is set properly
- *
- * CREATE TABLE sessions (
- * session_id varchar(32) NOT NULL,
- * session_last_access int(10) unsigned,
- * session_data text,
- * PRIMARY KEY (session_id)
- * );
- */
- class MY_Session {
- private $_lifetime;
- private $_sess_id_ttl;
- private $_match_ip;
- private $_match_useragent;
- private $_sess_db;
- private $_useDB;
- private $_sess_table;
- private $_flash_key = 'flash'; // prefix for "flash" variables (eg. flash:new:message)
- function __construct()
- {
- $this->object =& get_instance();
- // set config variables
- $this->_lifetime = $this->object->config->item('sess_expiration');
- $this->_sess_id_ttl = $this->object->config->item('sess_time_to_update');
- $this->_match_ip = $this->object->config->item('sess_match_ip');
- $this->_match_useragent = $this->object->config->item('sess_match_useragent');
- $this->_useDB = $this->object->config->item('sess_use_database');
- $this->_sess_table = $this->object->config->item('sess_table_name');
- log_message('debug', "Session Class Initialized");
- $this->_sess_run();
- }
- /**
- * Starts up the session system for current request
- */
- function _sess_run()
- {
- // Set session table and register this object as the session handler, if using databases
- if ($this->_useDB == TRUE) {
- session_set_save_handler(array(&$this, "_open"), array(&$this, "_close"), array(&$this, "_read"),
- array(&$this, "_write"),array(&$this, "_destroy"),array(&$this, "_gc"));
- }
- session_start();
- // if no lifetime set in config, set to 2 years
- if (!is_numeric($this->_lifetime) || $this->_lifetime <= 0) {
- $this->_lifetime = (60*60*24*365*2);
- }
- // if no session ID regeneration time set in config, set to 30 minutes
- if (!is_numeric($this->_sess_id_ttl) || $this->_sess_id_ttl <= 0) {
- $this->_sess_id_ttl = 1800;
- }
- // check if session has expired
- if ($this->_session_expired()) {
- $this->sess_destroy();
- return FALSE;
- }
- // match IP address if necessary
- if ($this->_match_ip == TRUE) {
- if ($this->_ips_match() == FALSE) {
- $this->sess_destroy();
- return FALSE;
- }
- }
- // match user agent if necessary
- if ($this->_match_useragent == TRUE) {
- if ($this->_useragents_match() == FALSE) {
- $this->sess_destroy();
- return FALSE;
- }
- }
- // regenerate session id if necessary
- // session data stays the same, but old session storage is destroyed
- if ( $this->_sess_id_expired() ) {
- $this->regenerate_id();
- }
- // delete old flashdata (from last request)
- $this->_flashdata_sweep();
- // mark all new flashdata as old (data will be deleted before next request)
- $this->_flashdata_mark();
- // finally, set last access time to now
- $this->set_userdata('sess_last_access', time());
- }
- /**
- * Checks if session has expired
- */
- function _session_expired()
- {
- // if this is the first time coming in, initialize access time
- if (!$this->userdata('sess_last_access')) {
- $this->set_userdata('sess_last_access', time());
- return FALSE;
- }
- $delta = time() - $this->userdata('sess_last_access');
- if ($delta >= $this->_lifetime ) {
- return true;
- }
- return false;
- }
- /**
- * Checks if stored IP matches current IP
- */
- function _ips_match() {
- // if this is the first time coming in, initialize IP address
- if (!$this->userdata('sess_ip_address')) {
- $this->set_userdata('sess_ip_address', $this->object->input->ip_address());
- return TRUE;
- }
- return $this->userdata('sess_ip_address') == $this->object->input->ip_address();
- }
- /**
- * Checks if stored user agent matches current user agent
- */
- function _useragents_match() {
- // if this is the first time coming in, initialize user agent
- if (!$this->userdata('sess_useragent')) {
- $this->set_userdata('sess_useragent', trim(substr($this->object->input->user_agent(), 0, 50)));
- return TRUE;
- }
- return $this->userdata('sess_useragent') == trim(substr($this->object->input->user_agent(), 0, 50));
- }
- /**
- * Checks if session id needs regenerating
- */
- function _sess_id_expired()
- {
- // if this is the first time coming in, initialize regenerated time
- if (!$this->userdata('sess_last_regenerated')) {
- $this->set_userdata('sess_last_regenerated', time());
- return false;
- }
- $delta = time() - $this->userdata('sess_last_regenerated');
- if ( $delta >= $this->_sess_id_ttl ) {
- return true;
- }
- return false;
- }
- /**
- * Regenerates session id
- */
- function regenerate_id()
- {
- // regenerate session id and store it
- // $delete_old_session parameter works in PHP5 only!
- session_regenerate_id(TRUE);
- // update the session generation time
- $this->set_userdata('sess_last_regenerated', time());
- }
- /**
- * Destroys the session and erases session storage
- */
- function sess_destroy()
- {
- session_unset();
- if ( isset( $_COOKIE[session_name()] ) )
- {
- //@@@ was having trouble just using setcookie() because it wasn't unsetting fast enough
- unset($_COOKIE[session_name()]);
- setcookie(session_name(), '', time()-42000, '/'); //@@@
- }
- session_destroy();
- }
- /**