/** * Обновление данных сессии * Важный момент: сессию обновляем в кеше и раз в 10 минут скидываем в БД */ protected function UpdateSession() { $this->oSession->setDateLast(date("Y-m-d H:i:s")); $this->oSession->setIpLast(func_getIp()); if (false === ($data = $this->Cache_Get("user_session_{$this->oSession->getUserId()}"))) { $data = array('time' => time(), 'session' => $this->oSession); } else { $data['session'] = $this->oSession; } if (!Config::Get('sys.cache.use') or $data['time'] < time() - 60 * 10) { $data['time'] = time(); $this->oMapper->UpdateSession($this->oSession); $this->Cache_Clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('user_session_update')); } $this->Cache_Set($data, "user_session_{$this->oSession->getUserId()}", array(), 60 * 60 * 24 * 4); }
/** * Обновление данных сессии * Важный момент: сессию обновляем в кеше и раз в 10 минут скидываем в БД */ protected function UpdateSession() { $this->oSession->setDateLast(F::Now()); $this->oSession->setIpLast(F::GetUserIp()); $sCacheKey = "user_session_{$this->oSession->getUserId()}"; // Используем кеширование по запросу if (false === ($data = E::ModuleCache()->Get($sCacheKey, true))) { $data = array('time' => time(), 'session' => $this->oSession); } else { $data['session'] = $this->oSession; } if ($data['time'] <= time()) { $data['time'] = time() + 600; $this->oMapper->UpdateSession($this->oSession); } E::ModuleCache()->Set($data, $sCacheKey, array('user_session_update'), 'PT20M', true); }
/** * Создание пользовательской сессии * * @param ModuleUser_EntityUser $oUser Объект пользователя * @param string $sKey Сессионный ключ * @return bool */ protected function CreateSession(ModuleUser_EntityUser $oUser, $sKey = null) { /** * Генерим новый ключ */ if (is_null($sKey)) { $sKey = md5(func_generator() . time() . $oUser->getId()); } /** * Проверяем ключ сессии */ if ($oSession = $this->oMapper->GetSessionByKey($sKey)) { /** * Если сессия уже не активна, то удаляем её */ if (!$oSession->isActive()) { $this->oMapper->DeleteSession($oSession); unset($oSession); } } if (!isset($oSession)) { /** * Проверяем количество активных сессий у пользователя и завершаем сверх лимита */ $iCountMaxSessions = Config::Get('module.user.count_auth_session'); $aSessions = $this->GetSessionsByUserId($oUser->getId()); $aSessions = array_slice($aSessions, $iCountMaxSessions - 1 < 0 ? 0 : $iCountMaxSessions - 1); foreach ($aSessions as $oSessionOld) { $oSessionOld->setDateClose(date("Y-m-d H:i:s")); $this->oMapper->UpdateSession($oSessionOld); } /** * Проверяем количество всех сессий у пользователя и удаляем сверх лимита */ $iCountMaxSessions = Config::Get('module.user.count_auth_session_history'); $aSessions = $this->GetSessionsByUserId($oUser->getId(), false); $aSessions = array_slice($aSessions, $iCountMaxSessions - 1 < 0 ? 0 : $iCountMaxSessions - 1); foreach ($aSessions as $oSessionOld) { $this->oMapper->DeleteSession($oSessionOld); } } $this->Cache_Clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('user_session_update')); $this->Cache_Delete("user_session_{$oUser->getId()}"); /** * Создаем новую или обновляем данные у старой */ if (!isset($oSession)) { $oSession = Engine::GetEntity('User_Session'); $oSession->setKey($sKey); $oSession->setIpCreate(func_getIp()); $oSession->setDateCreate(date("Y-m-d H:i:s")); $oSession->setExtraParam('user_agent', isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''); } $oSession->setUserId($oUser->getId()); $oSession->setIpLast(func_getIp()); $oSession->setDateLast(date("Y-m-d H:i:s")); if ($this->oMapper->CreateSession($oSession)) { $this->oSession = $oSession; return true; } return false; }