Skip to content

DbSession ​

Type
Class
Namespace
craft\web
Inherits
craft\web\DbSession » yii\web\DbSession » yii\web\MultiFieldSession » yii\web\Session » yii\base\Component » yii\base\BaseObject
Implements
ArrayAccess, Countable, IteratorAggregate, yii\base\Configurable
Since
5.11.0

Extends yii\web\DbSession to add defenses for “headers already sent” errors.

View source

Public Properties ​

PropertyDescription
allFlashesarray – Flash messages (key => message or key => [message1, message2]).
behaviors\yii\base\Behavior<static>[] – List of behaviors attached to this component.
cacheLimiterstring – Current cache limiter.
cookieParamsarray – The session cookie parameters.
countinteger – The number of session variables.
dbyii\db\Connection, array, string – The DB connection object or the application component ID of the DB connection.
flashstring – The key identifying the flash message.
flashParamstring – The name of the session variable that stores the flash message data.
gCProbabilityfloat – The probability (percentage) that the GC (garbage collection) process is started on every session initialization.
handlerSessionHandlerInterface, array – An object implementing the SessionHandlerInterface or a configuration array.
hasSessionIdboolean – Whether the current request has sent the session ID.
idstring – The current session ID.
isActiveboolean – Whether the session has started.
iteratoryii\web\SessionIterator – An iterator for traversing the session variables.
namestring – The current session name.
readCallbackcallable – A callback that will be called during session data reading.
savePathstring – The current session save path, defaults to '/tmp'.
sessionTablestring – The name of the DB table that stores the session data.
timeoutinteger – The number of seconds after which data will be seen as 'garbage' and cleaned up.
useCookiesboolean, null – The value indicating whether cookies should be used to store session IDs.
useCustomStorageboolean – Whether to use custom storage.
useStrictModeboolean – Whether strict mode is enabled or not.
useTransparentSessionIDboolean – Whether transparent sid support is enabled or not, defaults to false.
writeCallbackcallable, null – A callback that will be called during session data writing.

Protected Properties ​

PropertyDescription
_forceRegenerateIdstring, null – Holds the session id in case useStrictMode is enabled and the session id needs to be regenerated
_originalSessionModulestring, null – Holds 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.
fieldsarray – Session fields to be written into session table columns

Public Methods ​

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

has() ​

View source

Arguments ​

  • $key (mixed) – Session variable name

Returns ​

boolean – Whether there is the named session variable

Protected Methods ​

MethodDescription
composeFields()Composes storage field set for session writing.
extractData()Extracts session data from storage field set.
freeze()If session is started it's not possible to edit session ini settings. In PHP7.2+ it throws exception.
getReadQuery()Generates a query to get the session from db
registerSessionHandler()Registers session handler.
typecastFields()Method typecasts $fields before passing them to PDO.
unfreeze()Starts session and restores data from temporary variable
updateFlashCounters()Updates the counters for flash messages and removes outdated flash messages.