Skip to content
PHP Ecosystem
Explore

MultiFieldSession

API reference › yii › web › MultiFieldSession

1 subclass

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.

PropertyTypeDescriptionDefined by
$allFlashes read-onlyarrayFlash messages (key => message or key => [message1, message2]).Session
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$cacheLimiterstringCurrent cache limiter.Session
$cookieParamsarrayThe session cookie parameters.Session
$count read-onlyintThe number of session variables.Session
$flash write-onlystringThe key identifying the flash message.Session
$flashParamstringThe name of the session variable that stores the flash message data. Default: '__flash'.Session
$gCProbabilityfloatThe probability (percentage) that the GC (garbage collection) process is started on every session initialization.Session
$handlerSessionHandlerInterface|arrayAn object implementing the SessionHandlerInterface or a configuration array. Default: null.Session
$hasSessionIdboolWhether the current request has sent the session ID.Session
$idstringThe current session ID.Session
$isActive read-onlyboolWhether the session has started.Session
$iterator read-onlySessionIteratorAn iterator for traversing the session variables.Session
$namestringThe current session name.Session
$readCallbackcallableA callback that will be called during session data reading. Default: null.MultiFieldSession
$savePathstringThe current session save path, defaults to ‘/tmp’.Session
$timeoutintThe number of seconds after which data will be seen as ‘garbage’ and cleaned up.Session
$useCookiesbool|nullThe value indicating whether cookies should be used to store session IDs.Session
$useCustomStorage read-onlyboolWhether to use custom storage.MultiFieldSession
$useStrictModeboolWhether strict mode is enabled or not.Session
$useTransparentSessionIDboolWhether transparent sid support is enabled or not, defaults to false.Session
$writeCallbackcallable|nullA callback that will be called during session data writing. Default: null.MultiFieldSession
$_forceRegenerateId protectedstring|nullHolds the session id in case useStrictMode is enabled and the session id needs to be regenerated Default: null.Session
$_originalSessionModule protected staticstring|nullHolds the original session module (before a custom handler is registered) so that it can be restored when a Session component without custom handler is used after one that has. Default: null.Session
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__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
__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.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() staticReturns the fully qualified name of this class.yii\base\BaseObject
close()Ends the current session and store session data.Session
count()Returns the number of items in the session.Session
destroy()Frees all session variables and destroys all data registered to a session.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
get()Returns the session variable value with the session variable name.Session
getAllFlashes()Returns all flash messages.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 limiterSession
getCookieParams()Session
getCount()Returns the number of items in the session.Session
getFlash()Returns a flash message.Session
getGCProbability()Session
getHasSessionId()Returns a value indicating whether the current request has sent the session ID.Session
getId()Gets the session ID.Session
getIsActive()Session
getIterator()Returns an iterator for traversing the session variables.Session
getName()Gets the name of the current session.Session
getSavePath()Gets the current session save path.Session
getTimeout()Session
getUseCookies()Returns the value indicating whether cookies should be used to store session IDs.Session
getUseCustomStorage()Returns a value indicating whether to use custom session storage.MultiFieldSession
getUseStrictMode()Session
getUseTransparentSessionID()Session
has()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.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.Session
off()Detaches an existing event handler from this component.yii\base\Component
offsetExists()This method is required by the interface ArrayAccess.Session
offsetGet()This method is required by the interface ArrayAccess.Session
offsetSet()This method is required by the interface ArrayAccess.Session
offsetUnset()This method is required by the interface ArrayAccess.Session
on()Attaches an event handler to an event.yii\base\Component
open()Starts the session.Session
regenerateID()Updates the current session ID with a newly generated one.Session
remove()Removes a session variable.Session
removeAll()Removes all session variables.Session
removeAllFlashes()Removes all flash messages.Session
removeFlash()Removes a flash message.Session
set()Adds a session variable.Session
setCacheLimiter()Set cache limiterSession
setCookieParams()Sets the session cookie parameters.Session
setFlash()Sets a flash message.Session
setGCProbability()Session
setHasSessionId()Sets the value indicating whether the current request has sent the session ID.Session
setId()Sets the session ID.Session
setName()Sets the name for the current session.Session
setSavePath()Sets the current session save path.Session
setTimeout()Session
setUseCookies()Sets the value indicating whether cookies should be used to store session IDs.Session
setUseStrictMode()Session
setUseTransparentSessionID()Session
trigger()Triggers an event.yii\base\Component
composeFields() protectedComposes storage field set for session writing.MultiFieldSession
extractData() protectedExtracts session data from storage field set.MultiFieldSession
freeze() protectedIf session is started it’s not possible to edit session ini settings. In PHP7.2+ it throws exception.Session
registerSessionHandler() protectedRegisters session handler.Session
unfreeze() protectedStarts session and restores data from temporary variableSession
updateFlashCounters() protectedUpdates the counters for flash messages and removes outdated flash messages.Session
public callable $readCallback = null

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|null $writeCallback = null

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 function getUseCustomStorage(): bool

Returns a value indicating whether to use custom session storage.

This method overrides the parent implementation and always returns true.

Returns

bool: Whether to use custom storage.

protected function composeFields(string|null $id = null, string|null $data = null): array

Composes storage field set for session writing.

Parameters

ParameterTypeDescription
$idstring|nullOptional session id
$datastring|nullOptional session data

Returns

array: Storage fields

protected function extractData(array $fields): string

Extracts session data from storage field set.

Parameters

ParameterTypeDescription
$fieldsarrayStorage fields.

Returns

string: Session data.