0 follower

Abstract Class yii\web\MultiFieldSession

Inheritanceyii\web\MultiFieldSession » yii\web\Session » yii\base\Component » yii\base\BaseObject
ImplementsArrayAccess, Countable, IteratorAggregate, yii\base\Configurable
Subclassesyii\web\DbSession
Available since version2.0.6
Source Code https://github.com/yiisoft/yii2/blob/master/framework/web/MultiFieldSession.php

MultiFieldSession is the base class for session storage implementations with multi-field data storage support.

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 composeFields() method - while writing the session data into the storage and extractData() - while reading session data from the storage.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$allFlashes array Flash messages (key => message or key => [message1, message2]). yii\web\Session
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$cacheLimiter string yii\web\Session
$cookieParams array The session cookie parameters. yii\web\Session
$count integer The number of session variables yii\web\Session
$flash string The key identifying the flash message. yii\web\Session
$flashParam string The name of the session variable that stores the flash message data. yii\web\Session
$gCProbability float The probability (percentage) that the GC (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. yii\web\Session
$handler SessionHandlerInterface|array An object implementing the SessionHandlerInterface or a configuration array. yii\web\Session
$hasSessionId boolean Whether the current request has sent the session ID. yii\web\Session
$id string The current session ID yii\web\Session
$isActive boolean Whether the session has started yii\web\Session
$iterator yii\web\SessionIterator An iterator for traversing the session variables. yii\web\Session
$name string The current session name yii\web\Session
$readCallback callable A callback that will be called during session data reading. yii\web\MultiFieldSession
$savePath string The current session save path, defaults to '/tmp'. yii\web\Session
$timeout integer The number of seconds after which data will be seen as 'garbage' and cleaned up. yii\web\Session
$useCookies boolean|null The value indicating whether cookies should be used to store session IDs. yii\web\Session
$useCustomStorage boolean Whether to use custom storage. yii\web\MultiFieldSession
$useTransparentSessionID boolean Whether transparent sid support is enabled or not, defaults to false. yii\web\Session
$writeCallback callable A callback that will be called during session data writing. yii\web\MultiFieldSession

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
addFlash() Adds a flash message. yii\web\Session
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
close() Ends the current session and store session data. yii\web\Session
closeSession() Session close handler. yii\web\Session
count() Returns the number of items in the session. yii\web\Session
destroy() Frees all session variables and destroys all data registered to a session. yii\web\Session
destroySession() Session destroy handler. yii\web\Session
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
gcSession() Session GC (garbage collection) handler. yii\web\Session
get() Returns the session variable value with the session variable name. yii\web\Session
getAllFlashes() Returns all flash messages. yii\web\Session
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getCacheLimiter() Returns current cache limiter yii\web\Session
getCookieParams() yii\web\Session
getCount() Returns the number of items in the session. yii\web\Session
getFlash() Returns a flash message. yii\web\Session
getGCProbability() yii\web\Session
getHasSessionId() Returns a value indicating whether the current request has sent the session ID. yii\web\Session
getId() Gets the session ID. yii\web\Session
getIsActive() yii\web\Session
getIterator() Returns an iterator for traversing the session variables. yii\web\Session
getName() Gets the name of the current session. yii\web\Session
getSavePath() Gets the current session save path. yii\web\Session
getTimeout() yii\web\Session
getUseCookies() Returns the value indicating whether cookies should be used to store session IDs. yii\web\Session
getUseCustomStorage() Returns a value indicating whether to use custom session storage. yii\web\MultiFieldSession
getUseTransparentSessionID() yii\web\Session
has() yii\web\Session
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasFlash() Returns a value indicating whether there are flash messages associated with the specified key. yii\web\Session
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the application component. yii\web\Session
off() Detaches an existing event handler from this component. yii\base\Component
offsetExists() This method is required by the interface ArrayAccess. yii\web\Session
offsetGet() This method is required by the interface ArrayAccess. yii\web\Session
offsetSet() This method is required by the interface ArrayAccess. yii\web\Session
offsetUnset() This method is required by the interface ArrayAccess. yii\web\Session
on() Attaches an event handler to an event. yii\base\Component
open() Starts the session. yii\web\Session
openSession() Session open handler. yii\web\Session
readSession() Session read handler. yii\web\Session
regenerateID() Updates the current session ID with a newly generated one. yii\web\Session
remove() Removes a session variable. yii\web\Session
removeAll() Removes all session variables. yii\web\Session
removeAllFlashes() Removes all flash messages. yii\web\Session
removeFlash() Removes a flash message. yii\web\Session
set() Adds a session variable. yii\web\Session
setCacheLimiter() Set cache limiter yii\web\Session
setCookieParams() Sets the session cookie parameters. yii\web\Session
setFlash() Sets a flash message. yii\web\Session
setGCProbability() yii\web\Session
setHasSessionId() Sets the value indicating whether the current request has sent the session ID. yii\web\Session
setId() Sets the session ID. yii\web\Session
setName() Sets the name for the current session. yii\web\Session
setSavePath() Sets the current session save path. yii\web\Session
setTimeout() yii\web\Session
setUseCookies() Sets the value indicating whether cookies should be used to store session IDs. yii\web\Session
setUseTransparentSessionID() yii\web\Session
trigger() Triggers an event. yii\base\Component
writeSession() Session write handler. yii\web\Session

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
composeFields() Composes storage field set for session writing. yii\web\MultiFieldSession
extractData() Extracts session data from storage field set. yii\web\MultiFieldSession
freeze() If session is started it's not possible to edit session ini settings. In PHP7.2+ it throws exception. yii\web\Session
registerSessionHandler() Registers session handler. yii\web\Session
unfreeze() Starts session and restores data from temporary variable yii\web\Session
updateFlashCounters() Updates the counters for flash messages and removes outdated flash messages. yii\web\Session

Property Details

$readCallback public property

A callback that will be called during session data reading. The signature of the callback should be as follows:

function ($fields)

where $fields is the storage field set for read session and $session is this session instance. If callback returns an array, it will be merged into the session data.

For example:

function ($fields) {
    return [
        'expireDate' => Yii::$app->formatter->asDate($fields['expire']),
    ];
}
public callable $readCallback null
$useCustomStorage public read-only property

Whether to use custom storage.

$writeCallback public property

A callback that will be called during session data writing. The signature of the callback should be as follows:

function ($session)

where $session is this session instance, this variable can be used to retrieve session data. Callback should return the actual fields set, which should be saved into the session storage.

For example:

function ($session) {
    return [
        'user_id' => Yii::$app->user->id,
        'ip' => $_SERVER['REMOTE_ADDR'],
        'is_trusted' => $session->get('is_trusted', false),
    ];
}
public callable $writeCallback null

Method Details

composeFields() protected method

Composes storage field set for session writing.

protected array composeFields ( $id null, $data null )
$id string

Optional session id

$data string

Optional session data

return array

Storage fields

extractData() protected method

Extracts session data from storage field set.

protected string extractData ( $fields )
$fields array

Storage fields.

return string

Session data.

getUseCustomStorage() public method

Returns a value indicating whether to use custom session storage.

This method overrides the parent implementation and always returns true.

public boolean getUseCustomStorage ( )
return boolean

Whether to use custom storage.