Skip to content

Commit

Permalink
Handle session callback custom fields before session closed (yiisoft#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lubosdz committed Mar 5, 2019
1 parent 7813c26 commit acdb6b5
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions framework/web/DbSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class DbSession extends MultiFieldSession
*/
public $sessionTable = '{{%session}}';

/**
* @var array Session fields to be written into session table columns
*/
protected $fields = [];

/**
* Initializes the DbSession component.
Expand Down Expand Up @@ -136,6 +140,19 @@ public function regenerateID($deleteOldSession = false)
}
}

/**
* Ends the current session and store session data.
*/
public function close()
{
if ($this->getIsActive()) {
// prepare also write callback fields before session closed
$this->fields = $this->composeFields($this->id, $_SESSION);
$this->fields = $this->typecastFields($this->fields);
YII_DEBUG ? session_write_close() : @session_write_close();
}
}

/**
* Session read handler.
* @internal Do not call this method directly.
Expand Down Expand Up @@ -169,9 +186,9 @@ public function writeSession($id, $data)
// exception must be caught in session write handler
// https://secure.php.net/manual/en/function.session-set-save-handler.php#refsect1-function.session-set-save-handler-notes
try {
$fields = $this->composeFields($id, $data);
$fields = $this->typecastFields($fields);
$this->db->createCommand()->upsert($this->sessionTable, $fields)->execute();
if($this->fields){
$this->db->createCommand()->upsert($this->sessionTable, $fields)->execute();
}
} catch (\Exception $e) {
Yii::$app->errorHandler->handleException($e);
return false;
Expand Down

0 comments on commit acdb6b5

Please sign in to comment.