zoukankan      html  css  js  c++  java
  • session 丢失问题

    1. 存到memcached中, 十分简单, 在使用session之前, 加入下面两行代码

    int_set('session.save_handler', 'memcache');
    int_set('session.save_path', '127.0.0.1:11211');

    2.存到数据库里面, 重新定义了session的生命周期

    <?php
    /**
     * Class SessionDBTool
     */
    class SessionDBTool{
        private $link;    //数据库连接对象
        public function __construct(){
            //自定义session处理方法
            ini_set('session.save_handler','user');
            session_set_save_handler(
                array($this,'sess_open'),
                array($this,'sess_close'),
                array($this,'sess_read'),
                array($this,'sess_write'),
                array($this,'sess_destroy'),
                array($this,'sess_gc')
            );
            //开启session
            @session_start();
        }
    
         //开启session
        public function sess_open(){
            $this->link = MYSQLDB::getInstance($GLOBALS['config']['database']);
            //mysql_connect('localhost','root','root');
            //mysql_query('use shop');
        }
        public function sess_close(){
            return true;
        }
    
        //读取session
        public function sess_read($sess_id){
            $time = time();
            $expire = ini_get('session.gc_maxlifetime');
            $sql="select sess_data from it_session where sess_id='$sess_id' and $time - time < $expire";
            if($row=$this->link->fetchRow($sql)){
                return $row['sess_data'];
            }else{
                return '';
            }
        }
    
        //写入session
        public function sess_write($sess_id,$sess_data){
            $expire=time();
            //存在则更新
            $sql = "replace into it_session values('$sess_id','$expire', '$sess_data')";
            return $this->link->myquery($sql);
        }
    
        //销毁
        public function sess_destroy($sess_id){
            $sql="delete from it_session where sess_id='$sess_id'";
            return $this->link->myquery($sql);
        }
    
        //垃圾回收, 可以设置概率
        public function sess_gc($ttl){
            $now = time();
            $last = $now -$ttl;
            //删除过期session
            $sql = "delete from it_session where expire < $last";
            return $this->link->myquery($sql);
        }
    }
  • 相关阅读:
    滚动条滚动方向
    阶乘函数-尾递归
    返回顶部
    CommonJS
    vuessr
    随机字符串
    indexedDB
    深层次选择器
    Vue3.0简单替代Vuex
    shell 学习笔记
  • 原文地址:https://www.cnblogs.com/tanxing/p/6621966.html
Copyright © 2011-2022 走看看