# Session

> Session provides session data management and the related configurations.

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

| class | `yii\web\Session` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`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), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/web/Session.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/Session.php#L78) |

3 subclasses:

- [`CacheSession`](/docs/api/yii/web/cachesession/index.md)
- [`MultiFieldSession`](/docs/api/yii/web/multifieldsession/index.md)
- [`yii2\extensions\psrbridge\http\Session`](/docs/extensions/psr-bridge/reference/http/session/index.md)

Session provides session data management and the related configurations.

Session is a Web application component that can be accessed via `Yii::$app->session`.

To start the session, call [`open()`](/docs/api/yii/web/session/index.md#method-open); To complete and send out session data, call [`close()`](/docs/api/yii/web/session/index.md#method-close);
To destroy the session, call [`destroy()`](/docs/api/yii/web/session/index.md#method-destroy).

Session can be used like an array to set and get session data. For example,

```
$session = new Session;
$session->open();
$value1 = $session['name1'];  // get session variable 'name1'
$value2 = $session['name2'];  // get session variable 'name2'
foreach ($session as $name => $value) // traverse all session variables
$session['name3'] = $value3;  // set session variable 'name3'
```

Session can be extended to support customized session storage.
To do so, override [`$useCustomStorage`](/docs/api/yii/web/session/index.md#property-useCustomStorage) so that it returns true, and
override these methods with the actual logic about using custom storage:
`openSession()`, `closeSession()`, `readSession()`, `writeSession()`,
`destroySession()` and `gcSession()`.

Session also supports a special type of session data, called *flash messages*.
A flash message is available only in the current request and the next request.
After that, it will be deleted automatically. Flash messages are particularly
useful for displaying confirmation messages. To use flash messages, simply
call methods such as [`setFlash()`](/docs/api/yii/web/session/index.md#method-setFlash), [`getFlash()`](/docs/api/yii/web/session/index.md#method-getFlash).

For more details and usage information on Session, see the [guide article on sessions](/docs/guide/runtime-sessions-cookies/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-allFlashes"></span>`$allFlashes` read-only | `array` | Flash messages (key => message or key => [message1, message2]). | `Session` |
| <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` | `string` | Current cache limiter. | `Session` |
| <span id="property-cookieParams"></span>`$cookieParams` | `array` | The session cookie parameters. | `Session` |
| <span id="property-count"></span>`$count` read-only | `int` | The number of session variables. | `Session` |
| [`$flash`](#property-flash) write-only | `string` | The key identifying the flash message. | `Session` |
| <span id="property-flashParam"></span>`$flashParam` | `string` | The name of the session variable that stores the flash message data. Default: `'__flash'`. | `Session` |
| <span id="property-gCProbability"></span>`$gCProbability` | `float` | The probability (percentage) that the GC (garbage collection) process is started on every session initialization. | `Session` |
| [`$handler`](#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` |
| <span id="property-hasSessionId"></span>`$hasSessionId` | `bool` | Whether the current request has sent the session ID. | `Session` |
| <span id="property-id"></span>`$id` | `string` | The current session ID. | `Session` |
| <span id="property-isActive"></span>`$isActive` read-only | `bool` | Whether the session has started. | `Session` |
| <span id="property-iterator"></span>`$iterator` read-only | [`SessionIterator`](/docs/api/yii/web/sessioniterator/index.md) | An iterator for traversing the session variables. | `Session` |
| <span id="property-name"></span>`$name` | `string` | The current session name. | `Session` |
| <span id="property-savePath"></span>`$savePath` | `string` | The current session save path, defaults to '/tmp'. | `Session` |
| [`$timeout`](#property-timeout) | `int` | The number of seconds after which data will be seen as 'garbage' and cleaned up. | `Session` |
| <span id="property-useCookies"></span>`$useCookies` | `bool\|null` | The value indicating whether cookies should be used to store session IDs. | `Session` |
| <span id="property-useCustomStorage"></span>`$useCustomStorage` read-only | `bool` | Whether to use custom storage. | `Session` |
| <span id="property-useStrictMode"></span>`$useStrictMode` | `bool` | Whether strict mode is enabled or not. | `Session` |
| <span id="property-useTransparentSessionID"></span>`$useTransparentSessionID` | `bool` | Whether transparent sid support is enabled or not, defaults to false. | `Session` |
| <span id="property-_forceRegenerateId"></span>`$_forceRegenerateId` protected | `string\|null` | Holds the session id in case useStrictMode is enabled and the session id needs to be regenerated Default: `null`. | `Session` |
| <span id="property-_originalSessionModule"></span>`$_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` |

## 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) |
| [`addFlash()`](#method-addFlash) | Adds a flash message. | `Session` |
| <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. | `Session` |
| [`count()`](#method-count) | Returns the number of items in the session. | `Session` |
| [`destroy()`](#method-destroy) | Frees all session variables and destroys all data registered to a session. | `Session` |
| <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) |
| [`get()`](#method-get) | Returns the session variable value with the session variable name. | `Session` |
| [`getAllFlashes()`](#method-getAllFlashes) | Returns all flash messages. | `Session` |
| <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) |
| [`getCacheLimiter()`](#method-getCacheLimiter) | Returns current cache limiter | `Session` |
| [`getCookieParams()`](#method-getCookieParams) |  | `Session` |
| [`getCount()`](#method-getCount) | Returns the number of items in the session. | `Session` |
| [`getFlash()`](#method-getFlash) | Returns a flash message. | `Session` |
| [`getGCProbability()`](#method-getGCProbability) |  | `Session` |
| [`getHasSessionId()`](#method-getHasSessionId) | Returns a value indicating whether the current request has sent the session ID. | `Session` |
| [`getId()`](#method-getId) | Gets the session ID. | `Session` |
| [`getIsActive()`](#method-getIsActive) |  | `Session` |
| [`getIterator()`](#method-getIterator) | Returns an iterator for traversing the session variables. | `Session` |
| [`getName()`](#method-getName) | Gets the name of the current session. | `Session` |
| [`getSavePath()`](#method-getSavePath) | Gets the current session save path. | `Session` |
| [`getTimeout()`](#method-getTimeout) |  | `Session` |
| [`getUseCookies()`](#method-getUseCookies) | Returns the value indicating whether cookies should be used to store session IDs. | `Session` |
| [`getUseCustomStorage()`](#method-getUseCustomStorage) | Returns a value indicating whether to use custom session storage. | `Session` |
| [`getUseStrictMode()`](#method-getUseStrictMode) |  | `Session` |
| [`getUseTransparentSessionID()`](#method-getUseTransparentSessionID) |  | `Session` |
| [`has()`](#method-has) |  | `Session` |
| <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) |
| [`hasFlash()`](#method-hasFlash) | Returns a value indicating whether there are flash messages associated with the specified key. | `Session` |
| <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 application component. | `Session` |
| <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) |
| [`offsetExists()`](#method-offsetExists) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | `Session` |
| [`offsetGet()`](#method-offsetGet) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | `Session` |
| [`offsetSet()`](#method-offsetSet) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | `Session` |
| [`offsetUnset()`](#method-offsetUnset) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | `Session` |
| <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) |
| [`open()`](#method-open) | Starts the session. | `Session` |
| [`regenerateID()`](#method-regenerateID) | Updates the current session ID with a newly generated one. | `Session` |
| [`remove()`](#method-remove) | Removes a session variable. | `Session` |
| [`removeAll()`](#method-removeAll) | Removes all session variables. | `Session` |
| [`removeAllFlashes()`](#method-removeAllFlashes) | Removes all flash messages. | `Session` |
| [`removeFlash()`](#method-removeFlash) | Removes a flash message. | `Session` |
| [`set()`](#method-set) | Adds a session variable. | `Session` |
| [`setCacheLimiter()`](#method-setCacheLimiter) | Set cache limiter | `Session` |
| [`setCookieParams()`](#method-setCookieParams) | Sets the session cookie parameters. | `Session` |
| [`setFlash()`](#method-setFlash) | Sets a flash message. | `Session` |
| [`setGCProbability()`](#method-setGCProbability) |  | `Session` |
| [`setHasSessionId()`](#method-setHasSessionId) | Sets the value indicating whether the current request has sent the session ID. | `Session` |
| [`setId()`](#method-setId) | Sets the session ID. | `Session` |
| [`setName()`](#method-setName) | Sets the name for the current session. | `Session` |
| [`setSavePath()`](#method-setSavePath) | Sets the current session save path. | `Session` |
| [`setTimeout()`](#method-setTimeout) |  | `Session` |
| [`setUseCookies()`](#method-setUseCookies) | Sets the value indicating whether cookies should be used to store session IDs. | `Session` |
| [`setUseStrictMode()`](#method-setUseStrictMode) |  | `Session` |
| [`setUseTransparentSessionID()`](#method-setUseTransparentSessionID) |  | `Session` |
| <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) |
| [`freeze()`](#method-freeze) protected | If session is started it's not possible to edit session ini settings. In PHP7.2+ it throws exception. | `Session` |
| [`registerSessionHandler()`](#method-registerSessionHandler) protected | Registers session handler. | `Session` |
| [`unfreeze()`](#method-unfreeze) protected | Starts session and restores data from temporary variable | `Session` |
| [`updateFlashCounters()`](#method-updateFlashCounters) protected | Updates the counters for flash messages and removes outdated flash messages. | `Session` |

## Property details

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

```php
public string $flash
```

The key identifying the flash message.

Note that flash messages and normal
session variables share the same name space. If you have a normal session variable using the same name, its
value will be overwritten by this method.

**Access**

Write-only (virtual property).

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

```php
public SessionHandlerInterface|array $handler = null
```

An object implementing the SessionHandlerInterface or a configuration array.

If set, will be used to provide persistency instead of build-in methods.

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

```php
public int $timeout
```

The number of seconds after which data will be seen as 'garbage' and cleaned up.

The
default value is 1440 seconds (or the value of "session.gc_maxlifetime" set in php.ini).

## Method details

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

```php
public function addFlash(string $key, mixed $value = true, bool $removeAfterAccess = true): mixed
```

Adds a flash message.

If there are existing flash messages with the same key, the new one will be appended to the existing message array.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The key identifying the flash message. |
| `$value` | `mixed` | Flash message |
| `$removeAfterAccess` | `bool` | Whether the flash message should be automatically removed only if it is accessed. If false, the flash message will be automatically removed after the next request, regardless if it is accessed or not. If true (default value), the flash message will remain until after it is accessed. |

**Returns**

`mixed`

**See also**

- [`getFlash()`](/docs/api/yii/web/session/index.md#method-getFlash)
- [`setFlash()`](/docs/api/yii/web/session/index.md#method-setFlash)
- [`removeFlash()`](/docs/api/yii/web/session/index.md#method-removeFlash)

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

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

Ends the current session and store session data.

**Returns**

`mixed`

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

```php
public function count(): int
```

Returns the number of items in the session.

This method is required by [`Countable`](https://www.php.net/manual/en/class.countable.php) interface.

**Returns**

`int`: Number of items in the session.

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

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

Frees all session variables and destroys all data registered to a session.

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.

**Returns**

`mixed`

**See also**

- [`open()`](/docs/api/yii/web/session/index.md#method-open)
- [`$isActive`](/docs/api/yii/web/session/index.md#property-isActive)

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

```php
public function get(string $key, mixed $defaultValue = null): mixed
```

Returns the session variable value with the session variable name.

If the session variable does not exist, the `$defaultValue` will be returned.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The session variable name |
| `$defaultValue` | `mixed` | The default value to be returned when the session variable does not exist. |

**Returns**

`mixed`: The session variable value, or $defaultValue if the session variable does not exist.

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

```php
public function getAllFlashes(bool $delete = false): array
```

Returns all flash messages.

You may use this method to display all the flash messages in a view file:

```
<?php
foreach (Yii::$app->session->getAllFlashes() as $key => $message) {
    echo \yii\helpers\Html::tag(
        'div',
        \yii\helpers\Html::encode($message),
        [
            'class' => 'flash-' . \yii\helpers\Html::encode($key),
        ],
    );
} ?>
```

With the above code you can use the flash message key (for example `success`, `info`, `danger`) to style the
container with your own CSS framework. Both `$key` and `$message` are HTML-encoded to prevent cross-site
scripting when flash content comes from untrusted input.

Note that if you use [`addFlash()`](/docs/api/yii/web/session/index.md#method-addFlash), `$message` will be an array, and you will have to adjust the above code to
iterate over each entry and encode it individually.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$delete` | `bool` | Whether to delete the flash messages right after this method is called. If false, the flash messages will be automatically deleted in the next request. |

**Returns**

`array`: Flash messages (key => message or key => [message1, message2]).

**See also**

- [`setFlash()`](/docs/api/yii/web/session/index.md#method-setFlash)
- [`addFlash()`](/docs/api/yii/web/session/index.md#method-addFlash)
- [`getFlash()`](/docs/api/yii/web/session/index.md#method-getFlash)
- [`hasFlash()`](/docs/api/yii/web/session/index.md#method-hasFlash)
- [`removeFlash()`](/docs/api/yii/web/session/index.md#method-removeFlash)

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

```php
public function getCacheLimiter(): string
```

Returns current cache limiter

**Returns**

`string`: Current cache limiter

**Since**

2.0.14

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

```php
public function getCookieParams(): array
```

**Returns**

`array`: The session cookie parameters.

**See also**

- <https://www.php.net/manual/en/function.session-get-cookie-params.php>

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

```php
public function getCount(): int
```

Returns the number of items in the session.

**Returns**

`int`: The number of session variables

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

```php
public function getFlash(string $key, mixed $defaultValue = null, bool $delete = false): mixed
```

Returns a flash message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The key identifying the flash message |
| `$defaultValue` | `mixed` | Value to be returned if the flash message does not exist. |
| `$delete` | `bool` | Whether to delete this flash message right after this method is called. If false, the flash message will be automatically deleted in the next request. |

**Returns**

`mixed`: The flash message or an array of messages if addFlash was used

**See also**

- [`setFlash()`](/docs/api/yii/web/session/index.md#method-setFlash)
- [`addFlash()`](/docs/api/yii/web/session/index.md#method-addFlash)
- [`hasFlash()`](/docs/api/yii/web/session/index.md#method-hasFlash)
- [`getAllFlashes()`](/docs/api/yii/web/session/index.md#method-getAllFlashes)
- [`removeFlash()`](/docs/api/yii/web/session/index.md#method-removeFlash)

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

```php
public function getGCProbability(): float
```

**Returns**

`float`: The probability (percentage) that the GC (garbage collection) process is started on every session initialization.

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

```php
public function getHasSessionId(): bool
```

Returns a value indicating whether the current request has sent the session ID.

The default implementation will check cookie and $_GET using the session name.
If you send session ID via other ways, you may need to override this method
or call [`setHasSessionId()`](/docs/api/yii/web/session/index.md#method-setHasSessionId) to explicitly set whether the session ID is sent.

**Returns**

`bool`: Whether the current request has sent the session ID.

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

```php
public function getId(): string
```

Gets the session ID.

This is a wrapper for [PHP session_id()](https://www.php.net/manual/en/function.session-id.php).

**Returns**

`string`: The current session ID

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

```php
public function getIsActive(): bool
```

**Returns**

`bool`: Whether the session has started

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

```php
public function getIterator(): yii\web\SessionIterator
```

Returns an iterator for traversing the session variables.

This method is required by the interface [`IteratorAggregate`](https://www.php.net/manual/en/class.iteratoraggregate.php).

**Returns**

[`SessionIterator`](/docs/api/yii/web/sessioniterator/index.md): An iterator for traversing the session variables.

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

```php
public function getName(): string
```

Gets the name of the current session.

This is a wrapper for [PHP session_name()](https://www.php.net/manual/en/function.session-name.php).

**Returns**

`string`: The current session name

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

```php
public function getSavePath(): string
```

Gets the current session save path.

This is a wrapper for [PHP session_save_path()](https://www.php.net/manual/en/function.session-save-path.php).

**Returns**

`string`: The current session save path, defaults to '/tmp'.

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

```php
public function getTimeout(): int
```

**Returns**

`int`: The number of seconds after which data will be seen as 'garbage' and cleaned up. The default value is 1440 seconds (or the value of "session.gc_maxlifetime" set in php.ini).

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

```php
public function getUseCookies(): bool|null
```

Returns the value indicating whether cookies should be used to store session IDs.

**Returns**

`bool|null`: The value indicating whether cookies should be used to store session IDs.

**See also**

- [`setUseCookies()`](/docs/api/yii/web/session/index.md#method-setUseCookies)

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

```php
public function getUseCustomStorage(): bool
```

Returns a value indicating whether to use custom session storage.

This method should be overridden to return true by child classes that implement custom session storage.
To implement custom session storage, override these methods: `openSession()`, `closeSession()`,
`readSession()`, `writeSession()`, `destroySession()` and `gcSession()`.

**Returns**

`bool`: Whether to use custom storage.

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

```php
public function getUseStrictMode(): bool
```

**Returns**

`bool`: Whether strict mode is enabled or not.

**Since**

2.0.38

**See also**

- [`setUseStrictMode()`](/docs/api/yii/web/session/index.md#method-setUseStrictMode)

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

```php
public function getUseTransparentSessionID(): bool
```

**Returns**

`bool`: Whether transparent sid support is enabled or not, defaults to false.

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

```php
public function has(mixed $key): bool
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `mixed` | Session variable name |

**Returns**

`bool`: Whether there is the named session variable

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

```php
public function hasFlash(string $key): bool
```

Returns a value indicating whether there are flash messages associated with the specified key.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | Key identifying the flash message type |

**Returns**

`bool`: Whether any flash messages exist under specified key

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

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

Initializes the application component.

This method is required by IApplicationComponent and is invoked by application.

**Returns**

`mixed`

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

```php
public function offsetExists(int|string $offset): bool
```

This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | The offset to check on |

**Returns**

`bool`

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

```php
public function offsetGet(int|string $offset): mixed
```

This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | The offset to retrieve element. |

**Returns**

`mixed`: The element at the offset, null if no element is found at the offset

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

```php
public function offsetSet(int|string $offset, mixed $item): mixed
```

This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | The offset to set element |
| `$item` | `mixed` | The element value |

**Returns**

`mixed`

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

```php
public function offsetUnset(int|string $offset): mixed
```

This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | The offset to unset element |

**Returns**

`mixed`

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

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

Starts the session.

**Returns**

`mixed`

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

```php
public function regenerateID(bool $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` | `bool` | Whether to delete the old associated session file or not. |

**Returns**

`mixed`

**See also**

- [`open()`](/docs/api/yii/web/session/index.md#method-open)
- [`$isActive`](/docs/api/yii/web/session/index.md#property-isActive)

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

```php
public function remove(string $key): mixed
```

Removes a session variable.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The name of the session variable to be removed |

**Returns**

`mixed`: The removed value, null if no such session variable.

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

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

Removes all session variables.

**Returns**

`mixed`

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

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

Removes all flash messages.

Note that flash messages and normal session variables share the same name space.
If you have a normal session variable using the same name, it will be removed
by this method.

**Returns**

`mixed`

**See also**

- [`getFlash()`](/docs/api/yii/web/session/index.md#method-getFlash)
- [`setFlash()`](/docs/api/yii/web/session/index.md#method-setFlash)
- [`addFlash()`](/docs/api/yii/web/session/index.md#method-addFlash)
- [`removeFlash()`](/docs/api/yii/web/session/index.md#method-removeFlash)

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

```php
public function removeFlash(string $key): mixed
```

Removes a flash message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The key identifying the flash message. Note that flash messages and normal session variables share the same name space.  If you have a normal session variable using the same name, it will be removed by this method. |

**Returns**

`mixed`: The removed flash message. Null if the flash message does not exist.

**See also**

- [`getFlash()`](/docs/api/yii/web/session/index.md#method-getFlash)
- [`setFlash()`](/docs/api/yii/web/session/index.md#method-setFlash)
- [`addFlash()`](/docs/api/yii/web/session/index.md#method-addFlash)
- [`removeAllFlashes()`](/docs/api/yii/web/session/index.md#method-removeAllFlashes)

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

```php
public function set(string $key, mixed $value): mixed
```

Adds a session variable.

If the specified name already exists, the old value will be overwritten.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | Session variable name |
| `$value` | `mixed` | Session variable value |

**Returns**

`mixed`

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

```php
public function setCacheLimiter(string $cacheLimiter): mixed
```

Set cache limiter

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cacheLimiter` | `string` |  |

**Returns**

`mixed`

**Since**

2.0.14

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

```php
public function setCookieParams(array $value): mixed
```

Sets the session cookie parameters.

The cookie parameters passed to this method will be merged with the result
of `session_get_cookie_params()`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `array` | Cookie parameters, valid keys include: `lifetime`, `path`, `domain`, `secure`, `httponly` and `sameSite`. [ 'sameSite' => yii\web\Cookie::SAME_SITE_LAX, ] See https://owasp.org/www-community/SameSite for more information about `sameSite`. |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the parameters are incomplete.

**See also**

- <https://www.php.net/manual/en/function.session-set-cookie-params.php>

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

```php
public function setFlash(string $key, mixed $value = true, bool $removeAfterAccess = true): mixed
```

Sets a flash message.

A flash message will be automatically deleted after it is accessed in a request and the deletion will happen
in the next request.
If there is already an existing flash message with the same key, it will be overwritten by the new one.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The key identifying the flash message. Note that flash messages and normal session variables share the same name space. If you have a normal session variable using the same name, its value will be overwritten by this method. |
| `$value` | `mixed` | Flash message |
| `$removeAfterAccess` | `bool` | Whether the flash message should be automatically removed only if it is accessed. If false, the flash message will be automatically removed after the next request, regardless if it is accessed or not. If true (default value), the flash message will remain until after it is accessed. |

**Returns**

`mixed`

**See also**

- [`getFlash()`](/docs/api/yii/web/session/index.md#method-getFlash)
- [`addFlash()`](/docs/api/yii/web/session/index.md#method-addFlash)
- [`removeFlash()`](/docs/api/yii/web/session/index.md#method-removeFlash)

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

```php
public function setGCProbability(float $value): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `float` | The probability (percentage) that the GC (garbage collection) process is started on every session initialization. |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the value is not between 0 and 100.

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

```php
public function setHasSessionId(bool $value): mixed
```

Sets the value indicating whether the current request has sent the session ID.

This method is provided so that you can override the default way of determining
whether the session ID is sent.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `bool` | Whether the current request has sent the session ID. |

**Returns**

`mixed`

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

```php
public function setId(string $value): mixed
```

Sets the session ID.

This is a wrapper for [PHP session_id()](https://www.php.net/manual/en/function.session-id.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `string` | The session ID for the current session |

**Returns**

`mixed`

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

```php
public function setName(string $value): mixed
```

Sets the name for the current session.

This is a wrapper for [PHP session_name()](https://www.php.net/manual/en/function.session-name.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `string` | The session name for the current session, must be an alphanumeric string. It defaults to "PHPSESSID". |

**Returns**

`mixed`

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

```php
public function setSavePath(string $value): mixed
```

Sets the current session save path.

This is a wrapper for [PHP session_save_path()](https://www.php.net/manual/en/function.session-save-path.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `string` | The current session save path. This can be either a directory name or a [path alias](/docs/guide/concept-aliases/index.md). |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the path is not a valid directory

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

```php
public function setTimeout(int $value): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `int` | The number of seconds after which data will be seen as 'garbage' and cleaned up |

**Returns**

`mixed`

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

```php
public function setUseCookies(bool|null $value): mixed
```

Sets the value indicating whether cookies should be used to store session IDs.

Three states are possible:

- true: cookies and only cookies will be used to store session IDs.
- false: cookies will not be used to store session IDs.
- null: if possible, cookies will be used to store session IDs; if not, other mechanisms will be used (e.g. GET parameter)

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `bool\|null` | The value indicating whether cookies should be used to store session IDs. |

**Returns**

`mixed`

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

```php
public function setUseStrictMode(bool $value): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `bool` | Whether strict mode is enabled or not. When `true` this setting prevents the session component to use an uninitialized session ID. Note: Enabling `useStrictMode` on PHP &lt; 5.5.2 is only supported with custom storage classes. Warning! Although enabling strict mode is mandatory for secure sessions, the default value of 'session.use-strict-mode' is `0`. |

**Returns**

`mixed`

**Since**

2.0.38

**See also**

- <https://www.php.net/manual/en/session.configuration.php#ini.session.use-strict-mode>

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

```php
public function setUseTransparentSessionID(bool $value): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `bool` | Whether transparent sid support is enabled or not. |

**Returns**

`mixed`

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

```php
protected function freeze(): mixed
```

If session is started it's not possible to edit session ini settings. In PHP7.2+ it throws exception.

This function saves session data to temporary variable and stop session.

**Returns**

`mixed`

**Since**

2.0.14

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

```php
protected function registerSessionHandler(): mixed
```

Registers session handler.

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md)

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

```php
protected function unfreeze(): mixed
```

Starts session and restores data from temporary variable

**Returns**

`mixed`

**Since**

2.0.14

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

```php
protected function updateFlashCounters(): mixed
```

Updates the counters for flash messages and removes outdated flash messages.

This method should only be called once in [`init()`](/docs/api/yii/web/session/index.md#method-init).

**Returns**

`mixed`