年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3285|回复: 0

自定义Session的数据库实现

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2016-1-26 15:24:27 | 显示全部楼层 |阅读模式
    文件存储实现参考:http://www.yusian.com/thread-11083-1-1.html

    [PHP] 纯文本查看 复制代码
    <?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));
                    }
            }
    

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-12-22 11:54 , Processed in 0.069904 second(s), 22 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表