When considering the issue of session, I finally gave up on session:
1. The original session is managed using files. The quality of the file system directly affects the performance of the session, especially when there are several K people online at the same time. There are two solutions: database and file (using hash path).
2. The original session has poor scalability and controllability. Not conducive to integrating with my existing system.
3. When choosing a database, I did not choose sqlite. The last time I tested the efficiency of sqlite on win xp, it was not as good as expected.
Finally, I chose myql's heap table to process sessions, and combined session processing with online user statistics.
The code given below is an example of mine (no other details are provided, the specific use must be modified accordingly)
<?
/**
* Handle online users and simulate sessions
* CREATE TABLE `webqq_session` (
`sid` char( 32 ) NOT NULL ,
`uid` mediumint( 8 ) NOT NULL ,
`username` char( 80 ) NOT NULL ,
`ismember` tinyint( 1 ) NOT NULL ,
`logintime` int( 10 ) NOT NULL ,
`activetime` int( 10 ) NOT NULL ,
PRIMARY KEY (`sid`)
) ENGINE = HEAP DEFAULT CHARSET = gb2312
* @author:feifengxlq< http://www.phpobject.net > feifengxlq#gmail.com
* @since:2006-10-23
* @copyright:http://www.phpobject.net
* Note: The use of this file must be combined with other files: such as cookie filtering, some basic functions and database operation classes
*/
class session
{
var $mysql;
var $cookie_id='webqq_sid';
var $session=array();
var $max_time=1200;//The default maximum time is 20 minutes
function__construct()
{
$this->mysql=new module(TB_PREX.'_session');//Requires external support
$this->start();
}
function start()
{
if(empty($_COOKIE[$this->cookie_id]))
{
//Initialize session
$this->create();
}else{
//Cookie already exists, check if it exists in the database
$sid=$_COOKIE[$this->cookie_id];
if($this->mysql->detail('where sid=''.$sid.'''))
{
//Exist in database
$row['activetime']=time();
$this->mysql->update($row,'where sid=''.$sid.''');
$this->session=$this->mysql->detail('where sid=''.$sid.''');
}else{
//Does not exist in database
$this->create();
}
}
//Delete users who are not online in the database
$this->mysql->delete('where activetime+'.$this->max_time.'<'.time());
}
function register($name,$value,$update=false)
{
if(array_key_exists($name,$this->session)){
$this->session[$name]=$value;
}
if($update)$this->update();
}
function registry($name='')
{
if(empty($name)) return $this->session;
if(array_key_exists($name,$this->session)){
return $this->session[$name];
}
}
//Update session information in the database
function update()
{
$row['uid']=$this->session['uid'];
$row['username']=$this->session['username'];
$row['ismember']=$this->session['ismember'];
$row['logintime']=$this->session['logintime'];
$row['activetime']=$this->session['activetime'];
$this->mysql->update($row,'where sid=''.$this->session['sid'].''');
}
/*------------------The following are private methods------------------------- --------------------------------**/
functioncreate()
{
$nowtime=time();
$sid=md5('0'.$nowtime.getip());//The getip() function needs to be predefined: obtain the customer's IP address
setcookie($this->cookie_id,$sid,$nowtime+3600*24);//default 24 hours
$row['sid']=$sid;
$row['uid']=0;
$row['username']='guest';
$row['ismember']=0;
$row['logintime']=$nowtime;
$row['activetime']=$nowtime;
$this->mysql->add($row);//Write to the database
$this->session=$row;
}
}
?>