php实现将Session写入数据库_php技巧
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php实现将Session写入数据库_php技巧,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2930字,纯文字阅读大概需要5分钟。
内容图文
![php实现将Session写入数据库_php技巧](/upload/InfoBanner/zyjiaocheng/265/98406f851039434fbde2cbc687d4c49b.jpg)
<?php /* *@author Fahy *数据库为mysql, *数据库名为session,表名为session, *表中字段包括PHPSESSID,update_time,client_ip,data */ class Session{ private static $handler = null; private static $ip = null; private static $lifetime = null; private static $time = null; //配置静态变量 private static function init($handler){ self::$handler = $handler; //获取数据库资源 self::$ip = !empty($_SERVER["REMOTE_ADDR"])? $_SERVER["REMOTE_ADDR"]:'unkonw'; //获取客户端ip self::$lifetime = ini_get('session.gc_maxlifetime'); //获取session生命周期 self::$time = time(); //获取当前时间 } //调用session_set_save_handler()函数并开启session static function start($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(); } public static function open($path,$name){ return true; } public static function close(){ return true; } //查询数据库中的数据 public static function read($PHPSESSID){ $sql = "select PHPSESSID,update_time,client_ip,data from session where PHPSESSID=?"; $stmt = self::$handler->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::$time){ self::destroy($PHPSESSID); return ''; } return $result['data']; } /* *首先查询该session是否存在数据,如果存在,则更新数据,如果不存在,则插入数据 */ //将session写入数据库中,$data传入session中的keys和values数组 public static function write($PHPSESSID,$data){ $sql = "select PHPSESSID,update_time,client_ip,data from session where PHPSESSID=?"; $stmt = self::$handler->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_time<?"; $stmt = self::$handler->prepare($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);
以上所述就是本文的全部内容了,希望大家能够喜欢。
内容总结
以上是互联网集市为您收集整理的php实现将Session写入数据库_php技巧全部内容,希望文章能够帮你解决php实现将Session写入数据库_php技巧所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。