With multi-field data storage, session data can be split between several fields in the storage record. Using such a storage allows saving particular session data into separated field, which then can be used to manipulate sessions in the way plain PHP does not allow. For example the ID of the authenticated user can be saved as separated column in the MySQL 'session' table, which allows to query all active sessions for a particular user or terminate them at will. Customizing of the session writing is performed via [[writeCallback]], reading via [[readCallback]]. While extending this class you should use MultiFieldSession::composeFields method - while writing the session data into the storage and MultiFieldSession::extractData - while reading session data from the storage.
Since: 2.0.6
Author: Paul Klimov (klimov.paul@gmail.com)
Inheritance: extends yii\web\Session
Beispiel #1
0
 public function regenerateID($deleteOldSession = false)
 {
     $oldID = session_id();
     // if no session is started, there is nothing to regenerate
     if (empty($oldID)) {
         return;
     }
     parent::regenerateID(false);
     $newID = session_id();
     $row = (new Query())->from($this->sessionTable)->where(['session_id' => $oldID])->createCommand($this->db)->queryOne();
     if ($row !== false) {
         if ($deleteOldSession) {
             $this->db->createCommand()->update($this->sessionTable, ['session_id' => $newID], ['session_id' => $oldID])->execute();
         } else {
             $row['session_id'] = $newID;
             $this->db->createCommand()->insert($this->sessionTable, $row)->execute();
         }
     } else {
         $this->db->createCommand()->insert($this->sessionTable, $this->composeFields($newID, ''))->execute();
     }
 }
Beispiel #2
0
 /**
  * Updates the current session ID with a newly generated one.
  * Please refer to <http://php.net/session_regenerate_id> for more details.
  * @param boolean $deleteOldSession Whether to delete the old associated session file or not.
  */
 public function regenerateID($deleteOldSession = false)
 {
     $oldID = session_id();
     // if no session is started, there is nothing to regenerate
     if (empty($oldID)) {
         return;
     }
     parent::regenerateID(false);
     $newID = session_id();
     $collection = $this->db->getCollection($this->sessionCollection);
     $row = $collection->findOne(['id' => $oldID]);
     if ($row !== null) {
         if ($deleteOldSession) {
             $collection->update(['id' => $oldID], ['id' => $newID]);
         } else {
             unset($row['_id']);
             $row['id'] = $newID;
             $collection->insert($row);
         }
     } else {
         // shouldn't reach here normally
         $collection->insert($this->composeFields($newID, ''));
     }
 }