对此php提供了一个函数来解决
bool session_set_save_handler ( string open, string close, string read, string write, string destroy, string gc )
函数中一个参数对应一个函数,函数的名称自定但参数为固定格式,没有用到也要。
一、open(string save_path, string session_name)为session_start 参数string save_path session存取路径string session_name传递session id的cookie名字。
function _session_open(string save_path, string name)
{
$db=mysql_connect(localhost,root,123456,tendao);
return true;
}
二、close为session_close 无参数
在此对应关闭数据库,但一般网站中在此一般不要关闭。
三、read(key)为读取session键值。key对应session id。
四、write(key,date)其中的data对应设置的session变量,格式如下:
email_name|s:13:qqtxt@163.com;member_id|s:1:1;
五、destroy(key)注销session
在此程序段中对应删除对应记录项。
六、gc(expiry_time)清除过期session记录。
表的结构`session`
--
create table `session` (
`session_key` char(32) not null,
`session_data` char(255) not null,
`session_expiry` int(11) unsigned not null,
primary key (`session_key`)
) engine=myisam default charset=utf8;字符集根据情况更改
session();
}
function session(){
session_set_save_handler( array (& $this, _session_open),
array (& $this, _session_close),
array (& $this, _session_read),
array (& $this, _session_write),
array (& $this, _session_destroy),
array (& $this, _session_gc)
);
}
/**
* open session handler
*
* @param string $save_path
* @param string $session_name
* @return boolen
*/
function _session_open($save_path, $session_name)
{
$this->db=mysql_connect(localhost,root,123456) or die(数据库连接失败!);
mysql_select_db(tendao,$this->db);
return true;
}
function _session_close(){
return true;
}
function _session_read($key){
$expiry=time();
$s_query=sprintf(select session_data from session where session_key= '%s' and session_expiry > %d , $key, $expiry );
$result=mysql_query($s_query,$this->db);
$row=mysql_fetch_assoc($result);
if($row){
return $row['session_data'];
}
else
return false;
}
function _session_write($key,$data){
$expiry_time=time()+$this->expiry;
$s_query=sprintf(select session_data from session where session_key= '%s', $key);
$result=mysql_query($s_query,$this->db);
if(mysql_num_rows($result)==0){
$s_query=sprintf(insert into session values('%s','%s', %d),$key,$data,$expiry_time);
$result=mysql_query($s_query,$this->db);
}
else{
$s_query=sprintf(update session set session_key='%s', session_data='%s',session_expiry=%d where session_key='%s',$key,$data,$expiry_time,$key);
$result=mysql_query($s_query,$this->db);
}
return $result;
}
function _session_destroy($key){
$s_query=sprintf(delete from session where session_key= '%s', $key);
$result=mysql_query($s_query,$this->db);
return $result;
}
function _session_gc($expiry_time){
$expiry_time=time();
$s_query=sprintf(delete from session where session_expiry $result=mysql_query($s_query,$this->db);
return $result;
}
}
$_ses_=new session();
session_start();
$_session['time']=time()+1200;
echo strval(time()).'
'.strval($_session['time']);
?>
第零空间版权所有
http://www.bkjia.com/phpjc/478262.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/478262.htmltecharticle在php中session中的数据默认是以文件形式存储在磁盘上的,这对小型网站来说,可能已经可以满足要求了。大事对于大中型网站,或者一些有...