Sian 发表于 2016-1-26 15:24:27

自定义Session的数据库实现

文件存储实现参考:http://www.yusian.com/thread-11083-1-1.html

<?php
      class SASession{
                private static $pdo;
                public static function start(PDO $pdo){
                        
                        self::$pdo = $pdo;
                        // 设置session模式为用户模式,也可以在php.ini这个配置文件中修改
                        session_module_name("user");
                        // 注册过程
                        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(){
                        return true;
                }
                // 关闭
                public static function close(){
                        return true;
                }
                // 读
                public static function read($sid){
                        $sql = "select data, mtime from session where sid = ?";
                        $sta = self::$pdo ->prepare($sql);
                        $sta->execute(array($sid));
                        if($result = $sta->fetch(PDO::FETCH_ASSOC)){
                              // 如果超时则销毁返回空串
                              $time = time() - $result['mtime'];
                              $lifetime = ini_get("session.gc_maxlifetime");
                              if ($time > $lifetime){
                                       self::destroy($sid);
                                       return "";
                              }else{
                                        return $result['data'];
                              }
                        }else{
                              return "";
                        }
                }
                // 写
                public static function write($sid, $data){
                        $sql = "select data, mtime from session where sid = ?";
                        $sta = self::$pdo->prepare($sql);
                        $sta->execute(array($sid));
                        if($result = $sta->fetch(PDO::FETCH_ASSOC)){
                              if(time() - $result['mtime'] > 10 || $result['data'] != $data){
                                        // 修改
                                        $sql = "update session set data = ?, mtime = ? where sid = ?";
                                        $sta = self::$pdo -> prepare($sql);
                                        return $sta -> execute(array($data, time(), $sid));
                              }
                        }else{
                              // 插入
                              $sql = "insert into session(sid, data, mtime) values(?, ?, ?)";
                              $sta = self::$pdo->prepare($sql);
                              return $sta->execute(array($sid, $data, time()));
                        }
                }
                // 销毁
                public static function destroy($sid){
                        $sql = "delete from session where sid = ?";
                        $sta = self::$pdo->prepare($sql);
                        return $sta->execute(array($sid));
                }
                // 回收
                public static function gc($maxlifetime){
                        $lifetime = ini_get("session.gc_maxlifetime");
                        $time = time() - $lifetime;
                        $sql = "delete from session where mtime < ?";
                        $sta = self::$pdo->prepare($sql);
                        return $sta->execute(array($time));
                }
      }

页: [1]
查看完整版本: 自定义Session的数据库实现