Home >php教程 >php手册 >php实现将Session写入数据库

php实现将Session写入数据库

WBOY
WBOYOriginal
2016-06-06 19:51:17815browse

这篇文章主要介绍了php实现将Session写入数据库的相关资料,需要的朋友可以参考下

使用session_set_save_handler()函数,将Session的内容写入数据库

prepare($sql); $stmt->execute(array($PHPSESSID)); if(!$result = $stmt->fetch(PDO::FETCH_ASSOC)){ return ''; } if(self::$ip == $result['client_ip']){ self::destroy($PHPSESSID); return ''; } if(($result['update_time']+self::$lifetime)<: self::destroy return public static function write phpsessid from session where self::>prepare($sql); $stmt->execute(array($PHPSESSID)); if($result=$stmt->fetch(PDO::FETCH_ASSOC)){ if($result['data'] != $data || self::$time > ($result['update_time']+30)){ $sql = "update session set update_time=?,data=? where PHPSESSID = ?"; $stmt = self::$handler->prepare($sql); $stmt->execute(array($self::$time,$data,$PHPSESSID)); } }else{ if(!empty($data)){ try{ $sql = "insert into session(PHPSESSID,update_time,client_ip,data) values(?,?,?,?)"; }catch(PDOException $e){ echo $e->getMessage(); } $sth = self::$handler->prepare($sql); $sth->execute(array($PHPSESSID,self::$time,self::$ip,$data)); } } return true; } public static function destroy($PHPSESSID){ $sql = "delete from session where PHPSESSID = ?"; $stmt = self::$handler->prepare($sql); $stmt->execute(array($PHPSESSID)); return true; } public static function gc($lifetime){ $sql = "delete from session where update_timeprepare($sql); $stmt->execute(array(self::$time-$lifetime)); return true; } } //使用PDO连接数据库 try{ $pdo = new PDO("mysql:host=localhost;dbname=session","root","hwj193"); }catch(PDOException $e){ echo $e->getMessage(); } //传递数据库资源 Session::start($pdo);

以上所述就是本文的全部内容了,希望大家能够喜欢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn