搜索
首页php教程PHP源码php怎么把session保存到MySql数据库中

session我们多半是保存在服务器中呀,但是今天有一个功能就是需要把session保存在数据库中,这样可以实现同ie多浏览了,下面我们一起来看具体实现例子。

<script>ec(2);</script>

php中session默认的存储方式是硬盘,php也可以改变默认的存储方式。
主要使用到session_set_save_handler方法,下面分享下如何将session保存到MySql数据库中的具体代码。

1.建session表

 代码如下 复制代码
CREATE TABLE `session` (
  `sessionid` varchar(128) NOT NULL,
  `uid` int(11) NOT NULL,
  `data` mediumblob NOT NULL,
  `timestamp` int(11) NOT NULL,
  `ip` varchar(15) NOT NULL,
  PRIMARY KEY  (`sessionid`),
  KEY `time_session` (`timestamp`,`sessionid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

uid 是保留字段

2.自定义session类

 代码如下 复制代码

class CustomSession{
 private static $db_host="localhost";
 private static $db_user="root";
 private static $db_password="";
 private static $database="session";

 private $conn;

 public static function getInstance(){
  static $instance=null;
  if($instance==null){
   $instance=new CustomSession();
  }

  return $instance;
 }

 public function __construct(){
  session_set_save_handler(
  array($this,"open"),
  array($this,"close"),
  array($this,"read"),
  array($this,"write"),
  array($this,"destroy"),
  array($this,"gc")
  );
 }

 public function __destruct(){
  session_write_close();
 }

 public function open(){
  $this->conn=mysql_connect(CustomSession::$db_host,CustomSession::$db_user,CustomSession::$db_password);
  mysql_select_db(CustomSession::$database,$this->conn);
 }

 public function close(){

  mysql_close($this->conn);
 }

 public function read($id){
  $escaped_id=mysql_escape_string($id);
  $res=$this->query("select * from `session` where `sessionid`='$escaped_id'");
  if($row=mysql_fetch_assoc($res)){
   $this->query("update `session` set `timetamp`=UTC_TIMESTAMP() where `sessionid`='$escaped_id'");
   return $row['data'];
  }
  return "";
 }

 public function write($id,$data){
  $query="replace into `session` (`sessionid`,`data`,`ip`,`timestamp`) values ('%s','%s','%s',UNIX_TIMESTAMP(UTC_TIMESTAMP()))";
  $this->query(sprintf($query,mysql_escape_string($id),mysql_escape_string($data),$_SERVER["REMOTE_ADDR"]));//www.111cn.net
 }

 public function destroy($id){
  $escaped_id=mysql_escape_string($id);
  $res=$this->query("delete from `session` where `id`='$escaped_id'");
  return (mysql_affected_rows($res)==1);
 }

 public function gc($lifetime){
  $this->query("delete from `session` where UNIX_TIMESTAMP(UTC_TIMESTAMP())-`timestamp` > $lifetime");
 }

 public function query($query){
  $res=mysql_query($query,$this->conn);
  return $res;
 }
}

?>

3.测试程序

 代码如下 复制代码

include('./CustomSession.class.php');

CustomSession::getInstance();

session_start();

$_SESSION['username']='feng';

print_r($_SESSION);

?>

运行测试程序后,查看数据库可以发现session表中已经增加了session记录

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具