| 
 | 
 
将此文件放到自己的类库里,就相当于重写了Session.php类。 
 
MY_Session.php 
PHP复制代码  
 
<?php defined('BASEPATH') OR  exit('No direct script access allowed');
 
 
new Session_Memcache ;
session_start();
Class MY_Session  Extends CI_Session
 {
 
 
 /** 
  * Fetch the current session data if it exists 
  * 
  * @access public 
  * @return bool 
  */
 public function sess_read ()
 {
  $this->userdata = $_SESSION;
  
  return TRUE;
 }
 
 /** 
  * Write the session data 
  * 
  * @access public 
  * @return void 
  */
 public function sess_write ()
 {
  $_SESSION = array();
  foreach($this->userdata as $key => $val)
  {
   $_SESSION[$key] = $val;
  }
 }
 
 /** 
  * Create a new session 
  * 
  * @access public 
  * @return void 
  */
 public function sess_create ()
 {
  if(session_id() == '')
  {
   session_start();
  }
  $this->userdata = $_SESSION;
 }
 
 /** 
  * Update an existing session 
  * 
  * @access public 
  * @return void 
  */
 public function sess_update ()
 {
  $_SESSION = array();
  foreach($this->userdata as $key => $val)
  {
   $_SESSION[$key] = $val;
  }
  
 }
 
 /** 
  * Destroy the current session 
  * 
  * @access public 
  * @return void 
  */
 public function sess_destroy ($destroy = TRUE)
 {
  session_unset();
 }
 
 
 /** 
  * Does nothing for native sessions 
  * 
  * @access public 
  * @return void 
  */
 public function _sess_gc (){
  
  
 }
}
 
class Session_Memcache
 {
 public $CI = '';
 
 
 
 /** 
  +---------------------------------------------------------- 
  * Session有效时间 
  +---------------------------------------------------------- 
  */
 protected $lifeTime='';
 
 
 /** 
  +---------------------------------------------------------- 
  * memcache句柄 
  +---------------------------------------------------------- 
  */
 protected static  $hander;
 
 
 public $_memcache_conf = '';
 
 
 function __construct (){
 
  $this->CI =& get_instance ();
  ini_set('session.cookie_path',$this->CI->config->item('cookie_path'));
  ini_set('session.cookie_domain',$this->CI->config->item('cookie_domain'));
  ini_set('session.cookie_lifetime', $this->CI->config->item('cookie_lifetime'));
  $this->lifeTime = $this->CI->config->item('cookie_lifetime');
  $this->execute();
 }
 
 
 
 /** 
  +---------------------------------------------------------- 
  * 打开Session 
  +---------------------------------------------------------- 
  * @access public 
  +---------------------------------------------------------- 
  * @param string $savePath 
  * @param mixed $sessName 
  +---------------------------------------------------------- 
  */
 public function open ($savePath, $sessName) {
  
  
  
  $CI =& get_instance ();
  if ($CI->config->load('memcached', TRUE, TRUE))
  {
   if (is_array($CI->config->config['memcached']))
   {
    $this->_memcache_conf  = NULL;
  
    foreach ($CI->config->config['memcached'] as $name => $conf)
    {
     $this->_memcache_conf [$name] = $conf;
    }
   }
  }
  
  $memcache = new memcache ();
 
  
  foreach ($this->_memcache_conf  as $name => $cache_server)
  {
   if ( ! array_key_exists('hostname', $cache_server))
   {
    $cache_server['hostname'] = $this->_default_options ['default_host'];
   }
  
   if ( ! array_key_exists('port', $cache_server))
   {
    $cache_server['port'] = $this->_default_options ['default_port'];
   }
  
   if ( ! array_key_exists('weight', $cache_server))
   {
    $cache_server['weight'] = $this->_default_options ['default_weight'];
   }
  
   
   $hander = $memcache->addServer(
     $cache_server['hostname'], $cache_server['port'], $cache_server['weight']
   );
  }
  
  if(!$hander || !$memcache){
   
   return false;
  }
 
  
  self::$hander = $memcache;
  
  return true;
 }
 
 
 /** 
  +---------------------------------------------------------- 
  * 关闭Session 
  +---------------------------------------------------------- 
  * @access public 
  +---------------------------------------------------------- 
  */
 public function close () {
  return  self::$hander->close();
 }
 
 /** 
  +---------------------------------------------------------- 
  * 读取Session 
  +---------------------------------------------------------- 
  * @access public 
  +---------------------------------------------------------- 
  * @param string $sessID 
  +---------------------------------------------------------- 
  */
 public function read ($sessID) {
  $data =  self::$hander->get($sessID);
  if(!empty($data)){
   return $data;
  }
  return '';
 }
 
 /** 
  +---------------------------------------------------------- 
  * 写入Session 
  +---------------------------------------------------------- 
  * @access public 
  +---------------------------------------------------------- 
  * @param string $sessID 
  * @param String $sessData 
  +---------------------------------------------------------- 
  */
 public function write ($sessID,$sessData) {
  
  if(!$sessData){
   return false;
  }
  
  $result = self::$hander->set($sessID,$sessData,0,$this->lifeTime);
  if($result){
   return true;
  }
  return false;
 }
 
 /** 
  +---------------------------------------------------------- 
  * 删除Session 
  +---------------------------------------------------------- 
  * @access public 
  +---------------------------------------------------------- 
  * @param string $sessID 
  +---------------------------------------------------------- 
  */
 public function destroy ($sessID) {
  $result = self::$hander->delete($sessID);
  if($result){
   return true;
  }
  return false;
 }
 
 /** 
  +---------------------------------------------------------- 
  * Session 垃圾回收 
  +---------------------------------------------------------- 
  * @access public 
  +---------------------------------------------------------- 
  * @param string $sessMaxLifeTime 
  +---------------------------------------------------------- 
  */
 public function gc ($sessMaxLifeTime) {
  return true;
 }
 
 /** 
  +---------------------------------------------------------- 
  * 打开Session 
  +---------------------------------------------------------- 
  * @access public 
  +---------------------------------------------------------- 
  * @param string $savePath 
  * @param mixed $sessName 
  +---------------------------------------------------------- 
  */
 public function execute () {
  session_set_save_handler(array(&$this,"open"),
  array(&$this,"close"),
  array(&$this,"read"),
  array(&$this,"write"),
  array(&$this,"destroy"),
  array(&$this,"gc"));
 
 }
}
 
   复制代码 
 
然后在config目录下创建一个名字叫做memcached.php的文件,把下面代码贴进去即可。 
PHP复制代码  
 
<?php
// Memcache 配置
if (! defined ( 'BASEPATH' )){  
    exit ( 'No direct script access allowed' );
}
$config ['memcached'] = array (
    'hostname' => '192.168.1.54',
    'port' => 11211, 
    'weight' => 1 
);
 
 
   复制代码 
 
代码虽然写的很潦草,但是很实用,大家发现什么问题 请在此帖下方提出!谢谢 
 
 
 
 |   
 
 
 
 |