zoukankan      html  css  js  c++  java
  • 把session写到数据库中

     
    一、将SESSION数据写入数据库中(使用PDO)
     
    session_set_save_handler();
     
    pdo 
     
    1. 表设计 
     
    2. 用类编写完成,写入数据库的表中session
     
    3. 加入到实例中
     
     
    类中各回调函数定义都要定义为静态方法,属性也要是静态的(静态成员使用)
     
     
            定义的表字段:SESSIONID, update_time, client_ip, data
     
     
     
    2、具体程序代码
     
    1、定义的保存到数据库的session类:session.class.php
    <?php
    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"] : 'unknown';
    self::$lifetime=ini_get('session.gc_maxlifetime');
    self::$time=time();
    }
    
    static function start(PDO $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'];
    
    }
    
    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 = ?";
    
    $stm=self::$handler->prepare($sql);
    $stm->execute(array(self::$time, $data, $PHPSESSID));
    
    }
    }else{
    if(!empty($data)){
    $sql="insert into session(PHPSESSID, update_time, client_ip, data) values(?,?,?,?)";
    
    $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;
    }
    
    private static function gc($lifetime){
    $sql = "delete from session where update_time < ?";
    
    $stmt=self::$handler->prepare($sql);
    
    $stmt->execute(array(self::$time-$lifetime));
    
    return true;
    } 
    }
    
    try{
    $pdo=new PDO("mysql:host=localhost;dbname=xsphpdb", "root", "123456");
    }catch(PDOException $e){
    echo $e->getMessage();
    }
    
    Session::start($pdo);
    ?>
    
     
     
    2、session值定义页面:one.php
    <?php
    include "session.class.php";
    
    $_SESSION["isLogin3"]=1;
    $_SESSION["username"]="admin";
    $_SESSION["uid"]=333;
    
    echo session_name().'='.session_id().'<br>';
    ?>
    
     
     
    3、从另外页面取值的页面:two.php
    <?php
    include "session.class.php";
    
    print_r($_SESSION);
    echo '<br>';
    
    echo session_name().'='.session_id().'<br>';
    ?>
    
     
     
    4、销毁页面:three.php
    <?php
    include "session.class.php";
    
    $_SESSION=array();
    
    if(isset($_COOKIE[session_name()])){
    setCookie(session_name(), '', time()-100, '/');
    }
    
    session_destroy();
    
    echo session_name().'='.session_id().'<br>';
    ?>
    
  • 相关阅读:
    Python深入05 装饰器
    Python深入04 闭包
    Python深入03 对象的属性
    Ubuntu (虚拟机同样) 更换内核?
    .out
    GCC 编译详解
    linux 编译内核 /boot空间不足?
    Java Swing提供的文件选择对话框
    Java Swing 实时刷新JTextArea,以显示不断append的内容?
    为什么要编译Linux内核?
  • 原文地址:https://www.cnblogs.com/gxldan/p/4066853.html
Copyright © 2011-2022 走看看