Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果数据库会话数据被认为是旧的,如何清除它们?_Php - Fatal编程技术网

Php 如果数据库会话数据被认为是旧的,如何清除它们?

Php 如果数据库会话数据被认为是旧的,如何清除它们?,php,Php,我想在数据库会话数据的创建列数据过高时清除它们。如何成功地做到这一点 编辑: 一些代码: // starting a session public static function getIstance($_MYSESSION_CONF) { if (!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c; self::$

我想在数据库会话数据的创建列数据过高时清除它们。如何成功地做到这一点


编辑:

一些代码:

// starting a session
public static function getIstance($_MYSESSION_CONF)
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
            self::$instance->setUp($_MYSESSION_CONF);
        }

        return self::$instance;
    }

private function setUp($config) {

        $this->db_type = $config["DATABASE_TYPE"];
        $this->db_name = $config["DB_DATABASE"];
        $this->db_pass = $config["DB_PASSWORD"];
        $this->db_server = $config["DB_SERVER"];
        $this->db_username = $config["DB_USERNAME"];

        $this->table_name_session = $config["TB_NAME_SESSION"];
        $this->table_name_variable = $config["TB_NAME_VALUE"];
        $this->table_column_sid = $config["SID"];
        $this->table_column_name = $config["NAME"];
        $this->table_column_value = $config["VALUE"];
        $this->table_column_fexp = $config["FEXP"];
        $this->table_column_ua = $config["UA"];
        $this->table_column_exp = $config["EXP"];

        $this->sid_name = $config["SESSION_VAR_NAME"];
        $this->overwrite = ($config["OVERWRITE_PHP_FUNCTION"]=='1')?true:false;
        $this->sid_len = intval($config["SID_LEN"]);
        $this->session_duration = intval($config["DURATION"]);
        $this->session_max_duration = intval($config["MAX_DURATION"]);
        $this->use_cookie = ($config["USE_COOKIE"]=='1')?true:false;
        $this->encrypt_data = ($config["CRIPT"]=='1')?true:false;
        $this->encrypt_key = $config["CRIPT_KEY"];

        $this->hijackBlock = ($config["ENABLE_ANTI_HIJACKING"]=='1')?true:false;
        $this->hijackSalt = $config["ANTI_HIJACKING_SALT"];

        $this->dbConnection();
        $this->readSessionId();
        //check if i have to overwrite php
        if ($this->overwrite) { // it's true
            //yes.. i'm the best so i overwrite php function
            //Make sure session cookies expire when we want it to expires
            ini_set('session.cookie_lifetime', $this->session_duration);            
            //set the value of the garbage collector
            ini_set('session.gc_maxlifetime', $this->session_max_duration);
            // set the session name to our fantastic name
            ini_set('session.name', $this->sid_name);                        

            // register the new handler
            session_set_save_handler(
                array(&$this, 'open'),
                array(&$this, 'close'),
                array(&$this, 'read'),
                array(&$this, 'write'),
                array(&$this, 'destroy'),
                array(&$this, 'gc')
            );

//          register_shutdown_function('session_write_close');

            // start the session and cross finger
            session_id($this->getSessionId());
            session_start();
        }
    }
    ...
    function gc($maxlifetime)
    {
        $diff = time() - $this->session_max_duration;
        $this->SQLStatement_DeleteExpiredSession->bindParam(':time', $diff, PDO::PARAM_INT);
        if ($this->SQLStatement_DeleteExpiredSession->execute()===FALSE) {
            //trigger_error("Somenthing goes wrong with the garbace collector", E_USER_ERROR);
            trigger_error("Somenthing goes wrong with the garbace collector");
        } else {
            return true;
        }
    }

更新表上每次更新的时间戳

通过
$lifety=ini\u get('session.gc\u Maxlifety')获取会话的生存时间


从数据库中删除超过
updatedtimestamp+$lifetime

的行。如果您使用默认的php会话存储(文件系统),您不必担心,它将自动处理垃圾收集。但是,如果您想为会话实现数据库存储,请遵循我上面的回答。这很难实现!我的类文件中已经有gc和delete函数!我不能不看你的代码就说!给我一些代码来帮助你。顺便说一句,如果不存储时间戳,您将如何确定哪个是较老的用户&仍然是活动用户?