/** * Destroys all data registered to a session. * @return void */ public function destroy() { if (!self::$started) { throw new Nette\InvalidStateException('Session is not started.'); } session_destroy(); $_SESSION = NULL; self::$started = FALSE; if (!$this->response->isSent()) { $params = session_get_cookie_params(); $this->response->deleteCookie(session_name(), $params['path'], $params['domain'], $params['secure']); } }
/** * Starts and initializes session data. * * @throws Nette\InvalidStateException * @return void */ public function start() { if (self::$started) { return; } $this->configure($this->options); Nette\Diagnostics\Debugger::tryError(); session_start(); if (Nette\Diagnostics\Debugger::catchError($e) && !session_id()) { @session_write_close(); // this is needed throw new Nette\InvalidStateException('session_start(): ' . $e->getMessage(), 0, $e); } self::$started = true; /* structure: __NF: Counter, BrowserKey, Data, Meta, Time DATA: section->variable = data META: section->variable = Timestamp, Browser, Version */ unset($_SESSION['__NT'], $_SESSION['__NS'], $_SESSION['__NM']); // old unused structures // initialize structures $nf =& $_SESSION['__NF']; if (empty($nf)) { // new session $nf = array('C' => 0); } else { $nf['C']++; } // session regenerate every 30 minutes $nfTime =& $nf['Time']; $time = time(); if ($time - $nfTime > self::REGENERATE_INTERVAL) { $this->regenerated = $this->regenerated || isset($nfTime); $nfTime = $time; } // browser closing detection $browserKey = $this->request->getCookie('nette-browser'); if (!$browserKey) { $browserKey = Nette\Utils\Strings::random(); } $browserClosed = !isset($nf['B']) || $nf['B'] !== $browserKey; $nf['B'] = $browserKey; // resend cookie $this->sendCookie(); // process meta metadata if (isset($nf['META'])) { $now = time(); // expire section variables foreach ($nf['META'] as $section => $metadata) { if (is_array($metadata)) { foreach ($metadata as $variable => $value) { if (!empty($value['B']) && $browserClosed || !empty($value['T']) && $now > $value['T'] || isset($nf['DATA'][$section][$variable]) && is_object($nf['DATA'][$section][$variable]) && (isset($value['V']) ? $value['V'] : null) != Nette\Reflection\ClassType::from($nf['DATA'][$section][$variable])->getAnnotation('serializationVersion')) { if ($variable === '') { // expire whole section unset($nf['META'][$section], $nf['DATA'][$section]); continue 2; } unset($nf['META'][$section][$variable], $nf['DATA'][$section][$variable]); } } } } } if ($this->regenerated) { $this->regenerated = false; $this->regenerateId(); } register_shutdown_function(array($this, 'clean')); }