首先建立数据库表
在实验数据库sqldb中建立session表,用于存储数据
在根目录下建立需要用到的文件(重点是session,class.php这个类文件,包含列一些方法)
在session.class.php中主要用到的是session_set_save_handler()这个方法,借助pdo进行数据操作,用类编写写入数据库表中,
类中定义了一些静态方法,其属性也要为静态的,这样session的数据就直接写入数据库中,而不是保存在本地文件夹中
首先建立一个session类,类中首先定义一些私有静态的属性,定义了ip,生存时间和时间
private static function init($handler){ self::$handler=$handler; //代表pdo的链接 //ip先判断不为空 self::$ip=!empty($_server[remote_addr])? $_server[remote_addr] : 'unkown'; //从配置文件取出生存时间 self::$lifetime=ini_get('session.gc_maxlifetime'); self::$time=time(); }
接下来就是定义开启session的方法
//定义开启session的方法static function start(pdo $pdo){ self::init($pdo); //初始化私有方法 session_set_save_handler( array(__class__,open), array(__class__,close), array(__class__,read), array(__class__,write), array(__class__,destroy), array(__class__,gc) ); session_start();}
在开启session中有open, close, read, write, destory, gc 的方法,下面主要是定义出这些方法open() 和 close() 方法
public static function open($path, $name){ return true;}public static function close(){ return true;}
在定义这些方法时,最重要的是write() 和 read() 方法,因为这是直接从数据库读出或写入,采用pdo数据库预处理方式
read():先进行pdo预处理,然后在获取的一条记录中,要判断ip是否为数据库中的ip,取出的数据是否已经过期,都不是就成功读出
public static function read($phpsessid){ $sql=select phpsessid,update_time,client_ip,data from session where phpsessid= ?; //用?参数 //pdo预处理 $stmt=self::$handler->prepare($sql); $stmt->execute(array($phpsessid)); //获取一条记录 if(!$result=$stmt->fetch(pdo::fetch_assoc)){ return ''; } //判断当前访问ip是否为数据库存在的ip if(self::$ip != $result[client_ip]){ self::destroy($phpsessid); //销毁用户 return ''; } //判断是不是过期的 if(($result[update_time] + self::$lifetime) prepare($sql); $stmt->execute(array($phpsessid)); if($result=$stmt->fetch(pdo::fetch_assoc)){ //延迟30更新 if($result['data'] != $data || self::$time > ($result['update_time']+30)){ //更新数据语句 $sql=uptate session set update_time=?, data=? where phpsessid=?; $stm=self::$handler->prepare($sql); $stm->execute(array(self::$time, $data, $phpsessid)); } }else{ //判断传进来的数据是否为空,空时不插入 if(!empty($data)){ $sql=insert into session(phpsessid,update_time,client_ip,data) values(?,?,?,?); //插入值用?参数 $sth=self::$handler->prepare($sql); $sth->execute(array($phpsessid,self::$time,self::$ip,$data)); //必须用数组 } } return true; }
接下来就是数据的销毁
同样 destory() 和 gc()
destory():数据删除
gc():垃圾回收
public static function destroy($phpsessid){ $sql=delete from session where phpsessid=?; $stmt=self::$handler->prepare($sql); $stmt->execute(array($phpsessid)); return true; } private static function gc($lifetime){ $sql=delete from session where update_time prepare($sql); $stmt->execute(array(self::$time-$lifetime)); return true; } }
最后就抛出一个异常并调用session类
try{ $pdo=new pdo(mysql:host=localhost;dbname=sqldb,root,heyifeng19930924); }catch(pdoexception $e){ echo $e->getmessage(); } //调用session类 session::start($pdo);
在测试文件中,写法和session高级用法(即上一篇博客的测试文件)一样
只是在包含文件中包含这个类文件
即:includesession.class.php;
测试结果,如果插入数据成功,查询表格信息,在数据库中显示:
即传递列phpsessid的值
删除撤销后,查询表格显示
即撤销了phpsessid的值
http://www.bkjia.com/phpjc/971082.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/971082.htmltecharticlephp之将用户信息写入数据库 session高级应用将用户信息写入到数据库中 首先建立数据库表 在实验数据库sqldb中建立session表,用于存储数据...
