<?php
/*
http://www.vodcms.com
vodcms6.0
qq:24498936
msn:jdzcn_net@hotmail.com
Session存储方案定义
采用mysql保存session数据.
*/
class SessionModel extends Easy_Session_File{
    protected  static $table;
    public function init(){
        $this->start();
    }
    /*
    read 取seesion数据
    */
    public function read($sid){
        if($sid){
            $select = $this->_DB->select();
            $select->from(self::$table, 'session');
            $select->where("`sid`='$sid'");
            $select->limit(1);
            $sql = $select->toString();
            $result = $this->_DB->fetRow($sql);
            return $result['session'];
        }else{
            return false;
        }
    }
    public function write($sid, $data){
        $array = null;
        $select = $this->_DB->select();
        $select->from(self::$table, 'sid');
        $select->where("`sid`='$sid'");
        $select->limit(1);
        $sql = $select->toString();
        $result = $this->_DB->fetRow($sql);
        if ($result == false){
            $array['sid'] = $sid;
            $array['session'] = $data;
            $array['userip'] = $_SERVER['REMOTE_ADDR'];
            $array['lastime'] = time();
            $array['firstime'] = time();
            $this->_DB->insert(self::$table, $array);
        }else{
            $array = array();
            $array['session'] = $data;
            $array['userip'] = $_SERVER['REMOTE_ADDR'];
            $array['lastime'] = time();
            $this->_DB->update(self::$table, $array, array('sid'=>$sid));
            
        }
        unset($array);
    }
    function destroy(){
    }
    function __destruct()
    {
        //session_write_close();
    }
    public function gc(){
        $lifetime = 3600;
        $time = time();
        $this->_DB->delete(self::$table, 'lastime>firstime-'.$time);
    }
}
?>
