Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
perf(Session): Not resend set-cookies header for session
Browse files Browse the repository at this point in the history
Add pre-judgment of session exist status and not send set-cookies header
for session twice if session exist.
  • Loading branch information
Rhilip committed Mar 9, 2019
1 parent e22b04c commit 8fa31bc
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions framework/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ class Session extends Component
// SessionID长度
protected $_sessionIdLength = 26;

protected $_isNewSession = false;

// 请求前置事件
public function onRequestBefore()
{
parent::onRequestBefore();
// 载入session_id
$this->loadSessionId();
$this->_isNewSession = false;
$this->loadSessionId(); // 载入session_id
}

// 请求后置事件
Expand All @@ -69,7 +71,7 @@ public function loadSessionId()
{
$this->_sessionId = \Rid::app()->request->cookie($this->name);
if (is_null($this->_sessionId)) {
// 创建session_id
$this->_isNewSession = true;
$this->_sessionId = StringHelper::getRandomString($this->_sessionIdLength);
}
$this->_sessionKey = $this->saveKeyPrefix . $this->_sessionId;
Expand All @@ -91,7 +93,7 @@ public function set($name, $value)
{
$success = $this->saveHandler->hmset($this->_sessionKey, [$name => $value]);
$this->saveHandler->expire($this->_sessionKey, $this->maxLifetime);
$success and \Rid::app()->response->setCookie($this->name, $this->_sessionId, $this->cookieExpires, $this->cookiePath, $this->cookieDomain, $this->cookieSecure, $this->cookieHttpOnly);
$success and $this->_isNewSession and \Rid::app()->response->setCookie($this->name, $this->_sessionId, $this->cookieExpires, $this->cookiePath, $this->cookieDomain, $this->cookieSecure, $this->cookieHttpOnly);
return $success ? true : false;
}

Expand Down

0 comments on commit 8fa31bc

Please sign in to comment.