国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php框架 > codeigniter > CodeIgniter kndb session 用法简介

CodeIgniter kndb session 用法简介

来源:程序员人生   发布时间:2014-10-13 05:01:30 阅读次数:4927次

CI的session有点问题,很多时候需要挂载其他的扩展session,CI中国论坛网友推荐了kndb session,我大致用了一下,下面简单介绍下用法:
第一步:建立系统数据库,这里假设数据库名是test;
第二步:建立存储session的数据表sessions

CREATE TABLE sessions ( session_id varchar(32) NOT NULL, session_last_access int(10) unsigned, session_data text, PRIMARY KEY (session_id) );


第三步:修改CI文件夹/system/application/config/config.php

  1. $config['sess_cookie_name']            = 'CISESSION'
  2. $config['sess_expiration']            = 7200; 
  3. $config['sess_encrypt_cookie']        = FALSE; 
  4. $config['sess_table_name']            = 'sessions'
  5. $config['sess_match_ip']            = TRUE; 
  6. $config['sess_match_useragent']        = TRUE; 
  7. $config['sess_use_database']        = TRUE; 
  8. $config['sess_time_to_update']        = 300; 

第四步:在CI文件夹/system/application/libraries下建立文件Session.php,输入以下内容

  1. <?php  if (!defined('BASEPATH')) exit('No direct script access allowed'); 
  2. /** 
  3. * Code Igniter 
  4. * 
  5. * An open source application development framework for PHP 4.3.2 or newer 
  6. * 
  7. * @package     CodeIgniter 
  8. * @author      Elise Bosse 
  9. * @copyright   Copyright (c) 2008, E.Bosse 
  10. * @license     http://www.codeignitor.com/user_guide/license.html 
  11. * @link        http://www.codeigniter.com 
  12. * @since       Version 1.2 
  13. * @filesource 
  14. */ 
  15.   
  16. // ------------------------------------------------------------------------ 
  17.   
  18. /** 
  19. * Session class using native PHP session features and hardened against session fixation. 
  20. * Non-database part is based upon Dariusz Debowczyk's Native session library with some updates. 
  21. * The DB part makes use of PHP's session_set_save_handler() functionality 
  22. * This library is written for PHP 5 but it could be altered a bit to make it work in PHP4 
  23. * 
  24. * @package     CodeIgniter 
  25. * @subpackage  Libraries 
  26. * @category    Sessions 
  27. * @author      Elise Bosse 
  28. * @link        http://www.codeigniter.com/user_guide/libraries/sessions.html 
  29. * @class link  http://codeigniter.com/wiki/KNDB_Session/ 
  30. */ 
  31.   
  32. /** 
  33. * If using a database, create the following database table and make sure config file is set properly 
  34. * 
  35. * CREATE TABLE sessions ( 
  36. * session_id varchar(32) NOT NULL, 
  37. * session_last_access int(10) unsigned, 
  38. * session_data text, 
  39. * PRIMARY KEY (session_id) 
  40. * ); 
  41. */ 
  42.   
  43. class MY_Session { 
  44.   
  45.   private $_lifetime
  46.   private $_sess_id_ttl
  47.   private $_match_ip
  48.   private $_match_useragent
  49.   private $_sess_db;   
  50.   private $_useDB
  51.   private $_sess_table
  52.   private $_flash_key = 'flash'// prefix for "flash" variables (eg. flash:new:message) 
  53.   
  54.   function __construct() 
  55.   { 
  56.     $this->object =& get_instance(); 
  57.   
  58.     // set config variables 
  59.     $this->_lifetime = $this->object->config->item('sess_expiration'); 
  60.     $this->_sess_id_ttl = $this->object->config->item('sess_time_to_update'); 
  61.     $this->_match_ip = $this->object->config->item('sess_match_ip'); 
  62.     $this->_match_useragent = $this->object->config->item('sess_match_useragent'); 
  63.     $this->_useDB = $this->object->config->item('sess_use_database'); 
  64.     $this->_sess_table = $this->object->config->item('sess_table_name'); 
  65.   
  66.     log_message('debug'"Session Class Initialized"); 
  67.   
  68.     $this->_sess_run(); 
  69.   } 
  70.   
  71.   /** 
  72.    * Starts up the session system for current request 
  73.    */ 
  74.   function _sess_run() 
  75.   { 
  76.     // Set session table and register this object as the session handler, if using databases 
  77.     if ($this->_useDB == TRUE) { 
  78.       session_set_save_handler(array(&$this"_open"), array(&$this"_close"), array(&$this"_read"), 
  79.                    array(&$this"_write"),array(&$this"_destroy"),array(&$this"_gc")); 
  80.     } 
  81.   
  82.     session_start(); 
  83.   
  84.     // if no lifetime set in config, set to 2 years 
  85.     if (!is_numeric($this->_lifetime) || $this->_lifetime <= 0) { 
  86.       $this->_lifetime = (60*60*24*365*2); 
  87.     } 
  88.   
  89.     // if no session ID regeneration time set in config, set to 30 minutes 
  90.     if (!is_numeric($this->_sess_id_ttl) || $this->_sess_id_ttl <= 0) { 
  91.       $this->_sess_id_ttl = 1800; 
  92.     } 
  93.   
  94.     // check if session has expired 
  95.     if ($this->_session_expired()) { 
  96.       $this->sess_destroy(); 
  97.       return FALSE; 
  98.     } 
  99.   
  100.     // match IP address if necessary 
  101.     if ($this->_match_ip == TRUE) { 
  102.       if ($this->_ips_match() == FALSE) { 
  103.     $this->sess_destroy(); 
  104.     return FALSE; 
  105.       } 
  106.     } 
  107.      
  108.     // match user agent if necessary 
  109.     if ($this->_match_useragent == TRUE) { 
  110.       if ($this->_useragents_match() == FALSE) { 
  111.     $this->sess_destroy(); 
  112.     return FALSE; 
  113.       } 
  114.     } 
  115.   
  116.     // regenerate session id if necessary 
  117.     // session data stays the same, but old session storage is destroyed 
  118.     if ( $this->_sess_id_expired() ) {       
  119.       $this->regenerate_id(); 
  120.     } 
  121.   
  122.     // delete old flashdata (from last request) 
  123.     $this->_flashdata_sweep(); 
  124.   
  125.     // mark all new flashdata as old (data will be deleted before next request) 
  126.     $this->_flashdata_mark(); 
  127.   
  128.     // finally, set last access time to now 
  129.     $this->set_userdata('sess_last_access', time()); 
  130.   } 
  131.   
  132.   /** 
  133.    * Checks if session has expired 
  134.    */ 
  135.   function _session_expired() 
  136.   { 
  137.     // if this is the first time coming in, initialize access time 
  138.     if (!$this->userdata('sess_last_access')) { 
  139.       $this->set_userdata('sess_last_access', time()); 
  140.       return FALSE; 
  141.     } 
  142.   
  143.     $delta = time() - $this->userdata('sess_last_access'); 
  144.   
  145.     if ($delta  >=  $this->_lifetime ) { 
  146.       return true; 
  147.     } 
  148.   
  149.     return false; 
  150.   } 
  151.   
  152.   /** 
  153.    * Checks if stored IP matches current IP 
  154.    */ 
  155.   function _ips_match() { 
  156.     // if this is the first time coming in, initialize IP address 
  157.     if (!$this->userdata('sess_ip_address')) { 
  158.       $this->set_userdata('sess_ip_address',  $this->object->input->ip_address()); 
  159.       return TRUE; 
  160.     }     
  161.   
  162.     return $this->userdata('sess_ip_address') == $this->object->input->ip_address(); 
  163.   } 
  164.   
  165.   /** 
  166.    * Checks if stored user agent matches current user agent 
  167.    */ 
  168.   function _useragents_match() { 
  169.     // if this is the first time coming in, initialize user agent 
  170.     if (!$this->userdata('sess_useragent')) { 
  171.       $this->set_userdata('sess_useragent', trim(substr($this->object->input->user_agent(), 0, 50))); 
  172.       return TRUE; 
  173.     }     
  174.   
  175.     return $this->userdata('sess_useragent') == trim(substr($this->object->input->user_agent(), 0, 50)); 
  176.   } 
  177.   
  178.   
  179.   /** 
  180.    * Checks if session id needs regenerating 
  181.    */ 
  182.   function _sess_id_expired() 
  183.   { 
  184.     // if this is the first time coming in, initialize regenerated time 
  185.     if (!$this->userdata('sess_last_regenerated')) { 
  186.       $this->set_userdata('sess_last_regenerated', time()); 
  187.       return false; 
  188.     } 
  189.   
  190.     $delta = time() - $this->userdata('sess_last_regenerated'); 
  191.   
  192.     if ( $delta >=  $this->_sess_id_ttl ) { 
  193.       return true; 
  194.     } 
  195.   
  196.     return false; 
  197.   } 
  198.   
  199.   
  200.   /** 
  201.    * Regenerates session id 
  202.    */ 
  203.   function regenerate_id() 
  204.   { 
  205.     // regenerate session id and store it 
  206.     // $delete_old_session parameter works in PHP5 only! 
  207.     session_regenerate_id(TRUE); 
  208.   
  209.     // update the session generation time 
  210.     $this->set_userdata('sess_last_regenerated', time()); 
  211.   } 
  212.   
  213.   
  214.   /** 
  215.    * Destroys the session and erases session storage 
  216.    */ 
  217.   function sess_destroy() 
  218.   { 
  219.     session_unset(); 
  220.     if ( isset( $_COOKIE[session_name()] ) ) 
  221.       { 
  222.     //@@@ was having trouble just using setcookie() because it wasn't unsetting fast enough 
  223.     unset($_COOKIE[session_name()]); 
  224.     setcookie(session_name(), '', time()-42000, '/'); //@@@ 
  225.       } 
  226.   
  227.     session_destroy(); 
  228.   } 
  229.   
  230.   
  231. /**                                                       
    生活不易,码农辛苦
    如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
    程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生