php实现Session存储到Redis
对于大访问量的站点使用默认的Session并不合适,我们可以将其存入数据库、或者使用RedisKEY-VALUE数据存储方案
首先新建一个session表
CREATETABLE`sessions`( `sid`char(40)NOTNULL, `updatetime`int(20)NOTNULL, `data`varchar(200)NOTNULL, UNIQUEKEY`sid`(`sid`)USINGHASH )ENGINE=MEMORYDEFAULTCHARSET=utf8;
Mysql的memory引擎采用内存表,所有数据存储在内存,操作速度快
<?php //引入数据库文件 include"db.php"; classMySessionHandlerimplementsSessionHandlerInterface { private$savePath; private$sessData; public$expiretime;//设置过期时间 public$db;//数据库 publicfunction__construct($hanlder=''){ $this->db=Database::getInstance(); //获取数据库实力 ///var_dump($this->db); } publicfunctionopen($savePath,$sessionName) { returntrue; } publicfunctionclose() { returntrue; } publicfunctionread($id) { $sql="select*fromsessionswheresid='$id'"; $result=$this->db->execute($sql); if(!empty($result)){ return$this->sessData=$result; } } //函数的参数$id->当前会话ID //数据DATA->序列化之后的字符串 publicfunctionwrite($id,$data) { //echo$id; //echo$data; $now=time(); $newExp=$now+$this->expiretime;//总时间=当前时间+期限时间 $sql="select*fromsessionswheresid='$id'"; $result=$this->db->getOne($sql); //var_dump($result); if($data==''||isset($data)){ $data=$this->sessData; } if($result){ //如果存在则更新 $sql="updatesessionssetupdatetime='$newExp',data='$data'wheresid='$id'"; //echo$sql; $update_data=$this->db->execute($sql); if($update_data){ returntrue; } }else{ //不存在则生成生成 $sql="insertintosessions(sid,updatetime,data)values('$id','$now','$data')"; $insert_data=$this->db->execute($sql); if($insert_data){ returntrue; } } returnfalse; } publicfunctiondestroy($id) {//销毁 $sql="deletefromsessionswheresid="."$id"; $destory=$this->db->execute($sql); if($destory){ returntrue; }else{ returnfalse; } } publicfunctiongc($sessMaxLifeTime) { $t=time(); $sql="deletefromsessionswhere$t-'updatetime'>${sessMaxLifeTime}"; $data=$this->db->execute($this->tosql); if($data){ returntrue; }else{ returnfalse; } returntrue; } }
实例化
此处PHP手册可以有两种方法
1,实现了SessionHandlerInterface借口的对象,自PHP5.4可以使用
2,直接使用 session_set_save_handler
//判断PHP版本 if(version_compare(PHP_VERSION,5.4)==1){ 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"),array($session,"read"),array($session,"write"),array($session,"destory"),array($session,"gc")); session_start(); } $_SESSION['QQ']="QQ"; echo$_SESSION['QQ'];
数据库代码
<?php classDatabase{ static$instance; static$db; staticfunctiongetInstance(){ if(self::$instance){ returnself::$instance; }else{ returnnewDatabase(); } } publicfunction__construct(){ self::$db=newPDO('mysql:host=localhost;dbname=session','root',''); } publicfunctiongetOne($sql){ $rs=self::$db->query($sql); @$rs->setFetchMode(PDO::FETCH_ASSOC);//返回关联数组 $result=$rs->fetch(); return$result; } publicfunctionexecute($sql){ $rs=self::$db->exec($sql); return$rs; } } //$data=Database::getInstance(); //var_dump($data);
使用REDIS存储SESSION
<?php classSessionManager{ private$redis; private$sessionSavePath; private$sessionName; private$sessionExpireTime=30; publicfunction__construct(){ $this->redis=newRedis(); $this->redis->connect('127.0.0.1',6379);//连接redis $retval=session_set_save_handler( array($this,"open"), array($this,"close"), array($this,"read"), array($this,"write"), array($this,"destory"), array($this,"gc") ); session_start(); } publicfunctionopen($path,$name){ returntrue; } publicfunctionclose(){ returntrue; } publicfunctionread($id){ $value=$this->redis->get($id); if($value){ return$value; }else{ return""; } } publicfunctionwrite($id,$data){ if($this->redis->set($id,$data)){ $this->redis->expire($id,$this->sessionExpireTime); //设置过期时间 returntrue; } returnfalse; } publicfunctiondestory($id){ if($this->redis->delete($id)){ returntrue; } returnfalse; } publicfunctiongc($maxlifetime){ returntrue; } //析构函数 publicfunction__destruct(){ session_write_close(); } } $re=newSessionManager(); $_SESSION['name']="qq"; echo$_SESSION['name'];
以上就是详细的介绍了php实现Session存储到Redis的方法,希望对大家的学习有所帮助。