# DbSession

> DbSession extends yii\web\Session by using database as session data storage.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › DbSession

| class | `yii\web\DbSession` |
| --- | --- |
| Extends | [`MultiFieldSession`](/docs/api/yii/web/multifieldsession/index.md) » [`Session`](/docs/api/yii/web/session/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md), [`IteratorAggregate`](https://www.php.net/manual/en/class.iteratoraggregate.php), [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php), [`Countable`](https://www.php.net/manual/en/class.countable.php) |
| Since | 2.0 |
| Source | [`framework/web/DbSession.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/DbSession.php#L44) |

DbSession extends [`Session`](/docs/api/yii/web/session/index.md) by using database as session data storage.

By default, DbSession stores session data in a DB table named 'session'. This table
must be pre-created. The table name can be changed by setting [`$sessionTable`](/docs/api/yii/web/dbsession/index.md#property-sessionTable).

The following example shows how you can configure the application to use DbSession:
Add the following to your application config under `components`:

```
'session' => [
    'class' => 'yii\web\DbSession',
    // 'db' => 'mydb',
    // 'sessionTable' => 'my_session',
]
```

DbSession extends [`MultiFieldSession`](/docs/api/yii/web/multifieldsession/index.md), thus it allows saving extra fields into the [`$sessionTable`](/docs/api/yii/web/dbsession/index.md#property-sessionTable).
Refer to [`MultiFieldSession`](/docs/api/yii/web/multifieldsession/index.md) for more details.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-allFlashes"></span>[`$allFlashes`](/docs/api/yii/web/session/index.md#property-allFlashes) read-only | `array` | Flash messages (key => message or key => [message1, message2]). | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-cacheLimiter"></span>[`$cacheLimiter`](/docs/api/yii/web/session/index.md#property-cacheLimiter) | `string` | Current cache limiter. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-cookieParams"></span>[`$cookieParams`](/docs/api/yii/web/session/index.md#property-cookieParams) | `array` | The session cookie parameters. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-count"></span>[`$count`](/docs/api/yii/web/session/index.md#property-count) read-only | `int` | The number of session variables. | [`Session`](/docs/api/yii/web/session/index.md) |
| [`$db`](#property-db) | [`yii\db\Connection`](/docs/api/yii/db/connection/index.md)`\|array\|string` | The DB connection object or the application component ID of the DB connection. Default: `'db'`. | `DbSession` |
| <span id="property-flash"></span>[`$flash`](/docs/api/yii/web/session/index.md#property-flash) write-only | `string` | The key identifying the flash message. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-flashParam"></span>[`$flashParam`](/docs/api/yii/web/session/index.md#property-flashParam) | `string` | The name of the session variable that stores the flash message data. Default: `'__flash'`. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-gCProbability"></span>[`$gCProbability`](/docs/api/yii/web/session/index.md#property-gCProbability) | `float` | The probability (percentage) that the GC (garbage collection) process is started on every session initialization. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-handler"></span>[`$handler`](/docs/api/yii/web/session/index.md#property-handler) | [`SessionHandlerInterface`](https://www.php.net/manual/en/class.sessionhandlerinterface.php)`\|array` | An object implementing the SessionHandlerInterface or a configuration array. Default: `null`. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-hasSessionId"></span>[`$hasSessionId`](/docs/api/yii/web/session/index.md#property-hasSessionId) | `bool` | Whether the current request has sent the session ID. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-id"></span>[`$id`](/docs/api/yii/web/session/index.md#property-id) | `string` | The current session ID. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-isActive"></span>[`$isActive`](/docs/api/yii/web/session/index.md#property-isActive) read-only | `bool` | Whether the session has started. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-iterator"></span>[`$iterator`](/docs/api/yii/web/session/index.md#property-iterator) read-only | [`SessionIterator`](/docs/api/yii/web/sessioniterator/index.md) | An iterator for traversing the session variables. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-name"></span>[`$name`](/docs/api/yii/web/session/index.md#property-name) | `string` | The current session name. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-readCallback"></span>[`$readCallback`](/docs/api/yii/web/multifieldsession/index.md#property-readCallback) | `callable` | A callback that will be called during session data reading. Default: `null`. | [`MultiFieldSession`](/docs/api/yii/web/multifieldsession/index.md) |
| <span id="property-savePath"></span>[`$savePath`](/docs/api/yii/web/session/index.md#property-savePath) | `string` | The current session save path, defaults to '/tmp'. | [`Session`](/docs/api/yii/web/session/index.md) |
| [`$sessionTable`](#property-sessionTable) | `string` | The name of the DB table that stores the session data. Default: `'{{%session}}'`. | `DbSession` |
| <span id="property-timeout"></span>[`$timeout`](/docs/api/yii/web/session/index.md#property-timeout) | `int` | The number of seconds after which data will be seen as 'garbage' and cleaned up. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-useCookies"></span>[`$useCookies`](/docs/api/yii/web/session/index.md#property-useCookies) | `bool\|null` | The value indicating whether cookies should be used to store session IDs. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-useCustomStorage"></span>[`$useCustomStorage`](/docs/api/yii/web/multifieldsession/index.md#property-useCustomStorage) read-only | `bool` | Whether to use custom storage. | [`MultiFieldSession`](/docs/api/yii/web/multifieldsession/index.md) |
| <span id="property-useStrictMode"></span>[`$useStrictMode`](/docs/api/yii/web/session/index.md#property-useStrictMode) | `bool` | Whether strict mode is enabled or not. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-useTransparentSessionID"></span>[`$useTransparentSessionID`](/docs/api/yii/web/session/index.md#property-useTransparentSessionID) | `bool` | Whether transparent sid support is enabled or not, defaults to false. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-writeCallback"></span>[`$writeCallback`](/docs/api/yii/web/multifieldsession/index.md#property-writeCallback) | `callable\|null` | A callback that will be called during session data writing. Default: `null`. | [`MultiFieldSession`](/docs/api/yii/web/multifieldsession/index.md) |
| <span id="property-_forceRegenerateId"></span>[`$_forceRegenerateId`](/docs/api/yii/web/session/index.md#property-_forceRegenerateId) protected | `string\|null` | Holds the session id in case useStrictMode is enabled and the session id needs to be regenerated Default: `null`. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-_originalSessionModule"></span>[`$_originalSessionModule`](/docs/api/yii/web/session/index.md#property-_originalSessionModule) protected static | `string\|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. Default: `null`. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="property-fields"></span>`$fields` protected | `array` | Session fields to be written into session table columns Default: `[]`. Available since 2.0.17. | `DbSession` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-addFlash"></span>[`addFlash()`](/docs/api/yii/web/session/index.md#method-addFlash) | Adds a flash message. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`close()`](#method-close) | Ends the current session and store session data. | `DbSession` |
| <span id="method-count"></span>[`count()`](/docs/api/yii/web/session/index.md#method-count) | Returns the number of items in the session. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-destroy"></span>[`destroy()`](/docs/api/yii/web/session/index.md#method-destroy) | Frees all session variables and destroys all data registered to a session. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-get"></span>[`get()`](/docs/api/yii/web/session/index.md#method-get) | Returns the session variable value with the session variable name. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getAllFlashes"></span>[`getAllFlashes()`](/docs/api/yii/web/session/index.md#method-getAllFlashes) | Returns all flash messages. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getCacheLimiter"></span>[`getCacheLimiter()`](/docs/api/yii/web/session/index.md#method-getCacheLimiter) | Returns current cache limiter | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getCookieParams"></span>[`getCookieParams()`](/docs/api/yii/web/session/index.md#method-getCookieParams) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getCount"></span>[`getCount()`](/docs/api/yii/web/session/index.md#method-getCount) | Returns the number of items in the session. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getFlash"></span>[`getFlash()`](/docs/api/yii/web/session/index.md#method-getFlash) | Returns a flash message. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getGCProbability"></span>[`getGCProbability()`](/docs/api/yii/web/session/index.md#method-getGCProbability) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getHasSessionId"></span>[`getHasSessionId()`](/docs/api/yii/web/session/index.md#method-getHasSessionId) | Returns a value indicating whether the current request has sent the session ID. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getId"></span>[`getId()`](/docs/api/yii/web/session/index.md#method-getId) | Gets the session ID. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getIsActive"></span>[`getIsActive()`](/docs/api/yii/web/session/index.md#method-getIsActive) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getIterator"></span>[`getIterator()`](/docs/api/yii/web/session/index.md#method-getIterator) | Returns an iterator for traversing the session variables. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getName"></span>[`getName()`](/docs/api/yii/web/session/index.md#method-getName) | Gets the name of the current session. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getSavePath"></span>[`getSavePath()`](/docs/api/yii/web/session/index.md#method-getSavePath) | Gets the current session save path. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getTimeout"></span>[`getTimeout()`](/docs/api/yii/web/session/index.md#method-getTimeout) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getUseCookies"></span>[`getUseCookies()`](/docs/api/yii/web/session/index.md#method-getUseCookies) | Returns the value indicating whether cookies should be used to store session IDs. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getUseCustomStorage"></span>[`getUseCustomStorage()`](/docs/api/yii/web/multifieldsession/index.md#method-getUseCustomStorage) | Returns a value indicating whether to use custom session storage. | [`MultiFieldSession`](/docs/api/yii/web/multifieldsession/index.md) |
| <span id="method-getUseStrictMode"></span>[`getUseStrictMode()`](/docs/api/yii/web/session/index.md#method-getUseStrictMode) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-getUseTransparentSessionID"></span>[`getUseTransparentSessionID()`](/docs/api/yii/web/session/index.md#method-getUseTransparentSessionID) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-has"></span>[`has()`](/docs/api/yii/web/session/index.md#method-has) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasFlash"></span>[`hasFlash()`](/docs/api/yii/web/session/index.md#method-hasFlash) | Returns a value indicating whether there are flash messages associated with the specified key. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the DbSession component. | `DbSession` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-offsetExists"></span>[`offsetExists()`](/docs/api/yii/web/session/index.md#method-offsetExists) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-offsetGet"></span>[`offsetGet()`](/docs/api/yii/web/session/index.md#method-offsetGet) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-offsetSet"></span>[`offsetSet()`](/docs/api/yii/web/session/index.md#method-offsetSet) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-offsetUnset"></span>[`offsetUnset()`](/docs/api/yii/web/session/index.md#method-offsetUnset) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-open"></span>[`open()`](/docs/api/yii/web/session/index.md#method-open) | Starts the session. | [`Session`](/docs/api/yii/web/session/index.md) |
| [`regenerateID()`](#method-regenerateID) | Updates the current session ID with a newly generated one. | `DbSession` |
| <span id="method-remove"></span>[`remove()`](/docs/api/yii/web/session/index.md#method-remove) | Removes a session variable. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-removeAll"></span>[`removeAll()`](/docs/api/yii/web/session/index.md#method-removeAll) | Removes all session variables. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-removeAllFlashes"></span>[`removeAllFlashes()`](/docs/api/yii/web/session/index.md#method-removeAllFlashes) | Removes all flash messages. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-removeFlash"></span>[`removeFlash()`](/docs/api/yii/web/session/index.md#method-removeFlash) | Removes a flash message. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-set"></span>[`set()`](/docs/api/yii/web/session/index.md#method-set) | Adds a session variable. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setCacheLimiter"></span>[`setCacheLimiter()`](/docs/api/yii/web/session/index.md#method-setCacheLimiter) | Set cache limiter | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setCookieParams"></span>[`setCookieParams()`](/docs/api/yii/web/session/index.md#method-setCookieParams) | Sets the session cookie parameters. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setFlash"></span>[`setFlash()`](/docs/api/yii/web/session/index.md#method-setFlash) | Sets a flash message. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setGCProbability"></span>[`setGCProbability()`](/docs/api/yii/web/session/index.md#method-setGCProbability) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setHasSessionId"></span>[`setHasSessionId()`](/docs/api/yii/web/session/index.md#method-setHasSessionId) | Sets the value indicating whether the current request has sent the session ID. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setId"></span>[`setId()`](/docs/api/yii/web/session/index.md#method-setId) | Sets the session ID. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setName"></span>[`setName()`](/docs/api/yii/web/session/index.md#method-setName) | Sets the name for the current session. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setSavePath"></span>[`setSavePath()`](/docs/api/yii/web/session/index.md#method-setSavePath) | Sets the current session save path. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setTimeout"></span>[`setTimeout()`](/docs/api/yii/web/session/index.md#method-setTimeout) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setUseCookies"></span>[`setUseCookies()`](/docs/api/yii/web/session/index.md#method-setUseCookies) | Sets the value indicating whether cookies should be used to store session IDs. | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setUseStrictMode"></span>[`setUseStrictMode()`](/docs/api/yii/web/session/index.md#method-setUseStrictMode) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-setUseTransparentSessionID"></span>[`setUseTransparentSessionID()`](/docs/api/yii/web/session/index.md#method-setUseTransparentSessionID) |  | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-composeFields"></span>[`composeFields()`](/docs/api/yii/web/multifieldsession/index.md#method-composeFields) protected | Composes storage field set for session writing. | [`MultiFieldSession`](/docs/api/yii/web/multifieldsession/index.md) |
| <span id="method-extractData"></span>[`extractData()`](/docs/api/yii/web/multifieldsession/index.md#method-extractData) protected | Extracts session data from storage field set. | [`MultiFieldSession`](/docs/api/yii/web/multifieldsession/index.md) |
| <span id="method-freeze"></span>[`freeze()`](/docs/api/yii/web/session/index.md#method-freeze) protected | If session is started it's not possible to edit session ini settings. In PHP7.2+ it throws exception. | [`Session`](/docs/api/yii/web/session/index.md) |
| [`getReadQuery()`](#method-getReadQuery) protected | Generates a query to get the session from db | `DbSession` |
| <span id="method-registerSessionHandler"></span>[`registerSessionHandler()`](/docs/api/yii/web/session/index.md#method-registerSessionHandler) protected | Registers session handler. | [`Session`](/docs/api/yii/web/session/index.md) |
| [`typecastFields()`](#method-typecastFields) protected | Typecasts `$fields` before passing them to PDO. | `DbSession` |
| <span id="method-unfreeze"></span>[`unfreeze()`](/docs/api/yii/web/session/index.md#method-unfreeze) protected | Starts session and restores data from temporary variable | [`Session`](/docs/api/yii/web/session/index.md) |
| <span id="method-updateFlashCounters"></span>[`updateFlashCounters()`](/docs/api/yii/web/session/index.md#method-updateFlashCounters) protected | Updates the counters for flash messages and removes outdated flash messages. | [`Session`](/docs/api/yii/web/session/index.md) |

## Property details

### $db <span id="property-db"></span>

```php
public yii\db\Connection|array|string $db = 'db'
```

The DB connection object or the application component ID of the DB connection.

After the DbSession object is created, if you want to change this property, you should only assign it
with a DB connection object.
Starting from version 2.0.2, this can also be a configuration array for creating the object.

### $sessionTable <span id="property-sessionTable"></span>

```php
public string $sessionTable = '{{%session}}'
```

The name of the DB table that stores the session data.

The table should be pre-created as follows:

```
CREATE TABLE session
(
    id CHAR(40) NOT NULL PRIMARY KEY,
    expire INTEGER,
    data BLOB
)
```

where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type
that can be used for some popular DBMS:

- MySQL: LONGBLOB
- PostgreSQL: BYTEA
- MSSQL: BLOB

When using DbSession in a production server, we recommend you create a DB index for the 'expire'
column in the session table to improve the performance.

Note that according to the php.ini setting of `session.hash_function`, you may need to adjust
the length of the `id` column. For example, if `session.hash_function=sha256`, you should use
length 64 instead of 40.

## Method details

### close() <span id="method-close"></span>

```php
public function close(): mixed
```

Ends the current session and store session data.

**Returns**

`mixed`

**Since**

2.0.17

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the DbSession component.

This method will initialize the [`$db`](/docs/api/yii/web/dbsession/index.md#property-db) property to make sure it refers to a valid DB connection.

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if [`$db`](/docs/api/yii/web/dbsession/index.md#property-db) is invalid.

### regenerateID() <span id="method-regenerateID"></span>

```php
public function regenerateID(mixed $deleteOldSession = false): mixed
```

Updates the current session ID with a newly generated one.

Please refer to <https://www.php.net/session_regenerate_id> for more details.

This method has no effect when session is not [active](/docs/api/yii/web/session/index.md#method-getIsActive).
Make sure to call [`open()`](/docs/api/yii/web/session/index.md#method-open) before calling it.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$deleteOldSession` | `mixed` | Whether to delete the old associated session file or not. |

**Returns**

`mixed`

### getReadQuery() <span id="method-getReadQuery"></span>

```php
protected function getReadQuery(string $id): yii\db\Query
```

Generates a query to get the session from db

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | The id of the session |

**Returns**

[`yii\db\Query`](/docs/api/yii/db/query/index.md)

### typecastFields() <span id="method-typecastFields"></span>

```php
protected function typecastFields(array $fields): array
```

Typecasts `$fields` before passing them to PDO.

Applies the `data` column type cast resolved from the session table schema, so binary payloads bind correctly
across drivers (for example, MSSQL `varbinary`, which requires a `CONVERT` to avoid implicit conversion errors).

Override this method when special type casting is required.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fields` | `array` | Fields passed to PDO, keyed by column name. |

**Returns**

`array`: Fields with the `data` value cast for the active driver.

**Since**

2.0.13