如果认为数据库会话数据已经过时,如何清除数据库会话数据?

时间:2012-12-03 07:10:52

标签: php

我想在创建列数据太高时清除数据库会话数据。如何成功实现?


编辑:

一些代码:

// 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;
        }
    }

1 个答案:

答案 0 :(得分:0)

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

通过$lifetime = ini_get('session.gc_maxlifetime');

获取会话的生命时间

从db中删除超过updatedtimestamp + $lifetime

的行