Exemplo n.º 1
0
 public function write($sessID, $sessDara)
 {
     if (!$this->_isWrite) {
         return true;
     }
     if (!isset($_SESSION['sid']) || $_SESSION['sid'] != $sessID) {
         $_SESSION['sid'] = $sessID;
     }
     $newExpires = time() + $this->lifeTime;
     if (!$this->_sessions['expires'] || $newExpires >= $this->_sessions['expires'] + $this->updateTime) {
         $_SESSION['expires'] = $newExpires;
     }
     #$this->log("WRITE:".$sessID."",'sessions',1);
     #$this->log("OLD_SESSIONS:".var_export($this->_sessions,true)."",'sessions',1);
     #$this->log("SESSIONS:".var_export($_SESSION,true)."",'sessions',1);
     $arrChanges = array();
     foreach ($_SESSION as $key => $val) {
         if (isset($this->_sessions[$key])) {
             //不相同的数据 修改并记录
             if ($key != 'data' && $this->_sessions[$key] != $val) {
                 $this->_sessions[$key] = $val;
                 $arrChanges[$key] = $key;
             }
         } elseif (!isset($this->_sessions['data'][$key]) || $this->_sessions['data'][$key] != $val) {
             // 不存在SESSION字段中的数据 插入到DATA字段中 比较原因 不支持数组
             $this->_sessions['data'][$key] = $val;
             $arrChanges['data'] = 'data';
             #$this->log("CHANGE:"."{$key}",'sessions',1);
         }
     }
     //判断SESSION DATA数据是否被删除
     foreach ($this->_sessions['data'] as $key => $val) {
         if (!isset($_SESSION[$key])) {
             unset($this->_sessions['data'][$key]);
             $arrChanges['data'] = 'data';
             #$this->log("CHANGE:"."{$key}",'sessions',1);
         }
     }
     if (!empty($arrChanges)) {
         #$this->log("WRITECHANGES:".var_export($arrChanges,true),'sessions',1);
         if (!isset($arrChanges['expires'])) {
             $arrChanges['expires'] = 'expires';
             $this->_sessions['expires'] = time() + $this->lifeTime;
         }
         if (isset($arrChanges['sid'])) {
             //删除已有
             //$deleSql = "DELETE FROM sessions WHERE player_id='{$this->_sessions['player_id']}'";
             //$this->query($deleSql);
             $insertSql = "REPLACE INTO sessions (" . implode(',', $arrChanges) . ") VALUES (";
             $condition = "";
             foreach ($arrChanges as $cField) {
                 if ($cField == 'data') {
                     $cData = Com_System::s_json_encode($this->_sessions[$cField]);
                 } else {
                     $cData = $this->_sessions[$cField];
                 }
                 $condition .= ($condition ? ', ' : '') . "'{$cData}'";
             }
             $insertSql .= $condition . ")";
             $this->query($insertSql);
             #$this->log("WRITE:".$insertSql,'sessions',1);
         } else {
             $updateSql = "UPDATE sessions SET ";
             $condition = "";
             foreach ($arrChanges as $cField) {
                 if ($cField == 'data') {
                     $cData = Com_System::s_json_encode($this->_sessions[$cField]);
                 } else {
                     $cData = $this->_sessions[$cField];
                 }
                 $condition .= ($condition ? ', ' : '') . "{$cField} = '{$cData}'";
             }
             $condition .= " WHERE sid = '{$sessID}'";
             $updateSql .= $condition;
             $this->query($updateSql);
             #$this->log("WRITE:".$updateSql,'sessions',1);
         }
         $result = $this->affected_rows();
         if ($result >= 0) {
             if ($result > 0 && (isset($arrChanges['sid']) || isset($arrChanges['player_id']) || $this->_sessions['player_level'] < 15)) {
                 $result = $this->set_cache($this->get_cache_name($sessID), $this->_sessions, $this->expire);
                 #$this->log("WRITE CACHE: RESULT = {$result}",'sessions',1);
             } else {
                 $result = $this->delete_cache($this->get_cache_name($sessID));
                 #$this->log("DELETE CACHE: RESULT = {$result}",'sessions',1);
             }
         } else {
             return false;
         }
     }
     return true;
 }