最近想把session不在单独存储到文件中,想保存到数据库里面,
研究了一下:部分代码
protected function __construct($save_handler,$save_path,$hash_strategy){ ini_set('session.save_handler',$save_handler); ini_set('memcache.hash_strategy',$hash_strategy); ini_set('session.save_path',$save_path); self::$_mem=new Memcache();
$this->setMem();
} public static function GetInstance($save_handler='memcache',$save_path='tcp://127.0.0.1:11211',$hash_strategy='consistent'){ if(!self::$_instance){ self::$_instance=new self($save_handler,$save_path,$hash_strategy); } return self::$_instance; }
public function setMem($host='127.0.0.1',$port=11211){
self::$_mem->addServer($host,$port);
}
其实,如果我们写在别的数据库里面,或者我们单纯的想让他按照自己的方式存储
session_set_save_handler();可以帮助我们的;这是我的部分代码,其实这个最主要的就是了解这open,,read,close,write,destroy,gc这几个方法的参数值是什么,就ok了,了解他们什么时候调用
code
1 <?php 2 class SessionHandler { 3 4 public $memObject = null; //memcache操作对象句柄 5 private $_sessId = ''; 6 private $_sessKeyPrefix = 'sess_'; 7 private $_sessExpireTime = 86400; 8 private $_cookieDomain = '127.0.0.1'; //全域cookie域名 9 private $_sessionName = 'PHPSESSID'; 10 private $_cookieExpireTime = ''; 11 private $_memServers = array('127.0.0.1' => 11211); 12 private $_sessContainer = array(); //当前用户的session信息 13 private static $_instance = null; //本类单例对象 14 15 protected function __construct($host,$port){ 16 !empty($host) && $this->_memServers = array(trim($host) => $port); 17 ini_set('session.save_handler','user'); 18 $this->_initMemcacheObj(); 19 session_set_save_handler( 20 21 array($this, 'open'), //在运行session_start()时执行 22 23 array($this, 'close'), //在脚本执行完成或调用session_write_close() 或 session_destroy()时被执行,即在所有session操作完后被执行 24 25 array($this, 'read'), //在运行session_start()时执行,因为在session_start时,会去read当前session数据 26 27 array($this, 'write'), //此方法在脚本结束和使用session_write_close()强制提交SESSION数据时执行 28 29 array($this, 'destroy'), //在运行session_destroy()时执行 30 31 array($this, 'gc') //执行概率由session.gc_probability 和 session.gc_divisor的值决定,时机是在open,read之后,session_start会相继执行open,read和gc 32 ); 33 session_start(); 34 } 35 public static function getInstance($host='', $port=11211) { 36 #code 37 } 38 public function open(){ 39 #code 40 } 41 42 public function close(){ 43 return true; 44 } 45 public function read($key){ 46 #code 47 } 48 49 public function write($key,$varValue){ 50 #code 51 } 52 53 public function destroy(){ 54 #code 55 56 } 57 public function gc(){ 58 #code 59 } 60 /*将memcache 中的数据转化成数组的实行*/ 61 private function _unserializes($data_value) { 62 if(!$data_value) return false; 63 $result=''; 64 $vars = preg_split( 65 '/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\|/', 66 $data_value, -1, PREG_SPLIT_NO_EMPTY | 67 PREG_SPLIT_DELIM_CAPTURE 68 ); 69 for ($i = 0; isset($vars[$i]); $i++) { 70 $result[$vars[$i++]] = unserialize($vars[$i]); 71 } 72 return $result; 73 } 74 75 76 77 } 78 $db=SessionUser::getInstance('127.0.0.1',11211); $_SESSION['dopool']='dopoolcms'; $_SESSION['dopool22']='dopoolcms'; $_SESSION['do22']='dopoolcms'; ?>
这个呢,要了解session的保存之后的形式,ed|s:1:"6";我知道的也不是太多,欢迎批评!