sesion_start
session_id
session_name
session_save_path
session_save_handler
session_set_save_handler
session_set_cookie_params
1 <?php
2 ini_set("session.save_handler", "memcache");
3 ini_set("session.save_path", "tcp://127.0.0.1:12000");
4 session_start();
5 if (!isset($_SESSION['TEST'])) {
6 $_SESSION['TEST'] = time();
7 }
8 $_SESSION['TEST2'] = time();
9 print "test=>".$_SESSION['TEST'];
10 print "<br>";
11 print "test2=>".$_SESSION['TEST2'];
12 print "<br>";
13 print session_id();
14 echo "<br>";
15 $memcache = memcache_connect('localhost', 12000) or die("can't collect memcache");
16 $session_id=session_id();
17 var_dump($memcache->get($session_id));
18 echo "<br>";
19 $memcache->set('aaaa', 'hello everyone');
20 var_dump($memcache->get('aaaa'));
21 ?>
<?php
class MS{
static $mem;
static $maxtime;
function __construct($mem){
self::$mem = $mem;
self::$maxtime = ini_get('session.gc_maxlifetime');
session_module_name('user');//session文件保存方式,这个是必须的!除非在Php.ini文件中设置了
session_set_save_handler(
array(__CLASS__,'open'),//在运行session_start()时执行
array(__CLASS__,'close'),//在脚本执行完成或调用session_write_close() 或 session_destroy()时被执行,即在所有session操作完后被执行
array(__CLASS__,'read'),//在运行session_start()时执行,因为在session_start时,会去read当前session数据
array(__CLASS__,'write'),//此方法在脚本结束和使用session_write_close()强制提交SESSION数据时执行
array(__CLASS__,'destroy'),//在运行session_destroy()时执行
array(__CLASS__,'gc')//执行概率由session.gc_probability 和 session.gc_divisor的值决定,时机是在open,read之后,session_start会相继执行open,read和gc
);
session_start();//这也是必须的,打开session,必须在session_set_save_handler后面执行
}
static function open($sid){
return true;
}
static function close(){
return true;
}
static function read($sid){
return self::$mem->get($sid);
}
function write($sid,$data){
return self::$mem->set($sid,$data,MEMCACHE_COMPRESSED,self::$maxtime);
}
function destroy($sid){
return self::$mem->delete($sid);
}
function gc($maxtime){
return true;
}
}
$mem=new Memcache();
$mem->connect('127.0.0.1', '12000') or die("not collect");
$session=new MS($mem);
$_SESSION['007']='www.i007xd.com';
//session_destroy();
echo $_SESSION['007'];
echo "<br>";
echo session_id();
echo "<br>";
$session_id=session_id();
var_dump($mem->get($session_id));
?>