对于大访问量的站点使用默认的Session 并不合适,我们可以将其存入数据库、或者使用Redis KEY-VALUE数据存储方案 首先新建一个session表
MysqL 的memory引擎采用内存表,所有数据存储在内存,操作速度快
$this->db = Database::getInstance();
//<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/shujuku/" target="_blank" class="keywords">数据库</a>实力
///var_dump($this->db);
}
public function open($savePath,$sessionName)
{
return true;
}
public function close()
{
return true;
}
public function read($id)
{
$sql ="select from sessions where sid ='$id'";
$result = $this->db->execute($sql);
if(!empty($result)){
return $this->sessData = $result;
}
}
//函数的参数 $id -> 当前会话ID
//数据DATA -> 序列化之后的字符串
public function write($id,$data)
{
// echo $id;
// echo $data;
$now = time();
$newExp = $now+$this->expiretime; //总时间=当前时间 + 期限时间
$sql = "select from sessions where sid ='$id'";
$result = $this->db->getOne($sql);
//var_dump($result);
if($data==''||isset($data)){
$data = $this->sessData;
}
if($result){
//如果存在则更新
$sql ="update sessions set updatetime = '$newExp',data ='$data' where sid = '$id'";
//echo $sql;
$update_data =$this->db->execute($sql);
if($update_data){
return true;
}
}else{
//不存在则<a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" class="keywords">生成</a><a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" class="keywords">生成</a>
$sql = "insert into sessions(sid,updatetime,data) values('$id','$now','$data')";
$insert_data = $this->db->execute($sql);
if($insert_data){
return true;
}
}
return false;
}
public function destroy($id)
{ //销毁
$sql = "delete from sessions where sid="."$id";
$destory = $this->db->execute($sql);
if($destory){
return true;
}else{
return false;
}
}
public function gc($sessMaxLifeTime)
{
$t = time();
$sql ="delete from sessions where $t - 'updatetime'>${sessMaxLifeTime}";
$data = $this->db->execute($this->tosql);
if($data){
return true;
}else{
return false;
}
return true;
}
}
实例化
此处 PHP 手册可以有两种方法 1,实现了SessionHandlerInterface借口的对象,自PHP5.4可以使用 2 ,直接使用 session_set_save_handler
session_set_save_handler($handler,true);
session_start();
}else{
ini_set('session.use_trans_sid',0);
ini_set('session.use_cookies',1);
ini_set('session.cookie_path','/');
ini_set('session.save_handler','user');
session_module_name('user');
session_set_save_handler(array($session,"open"),array($session,"close"),"read"),"write"),"destory"),"gc"));
session_start();
}
$_SESSION['QQ']="QQ";
echo $_SESSION['QQ'];
public function getOne($sql){
$rs =self::$db->query($<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>);
@$rs->setFetchMode(PDO::FETCH_ASSOC);//返回关联数组
$result = $rs -> fetch();
return $result;
}
public function execute($<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>){
$rs = self::$db->exec($<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>);
return $rs;
}
}
//$data = Database::getInstance();
//var_dump($data);
使用REDIS 存储SESSION
public function open($path,$name){
return true;
}
public function close(){
return true;
}
public function read($id){
$value = $this->redis->get($id);
if($value){
return $value;
}else{
return "";
}
}
public function write($id,$data){
if($this->redis->set($id,$data)){
$this->redis->expire($id,$this->sessionExpireTime);
//设置过期时间
return true;
}
return false;
}
public function destory($id){
if($this->redis->delete($id)){
return true;
}
return false;
}
public function gc($maxlifetime){
return true;
}
//析构<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>
public function __destruct(){
session_write_close();
}
}
$re = new SessionManager();
$_SESSION['name'] = "qq";
echo $_SESSION['name'];