Skip to content

Session ​

Type
Class
Namespace
craft\helpers
Inherits
craft\helpers\Session
Since
3.6.9

Class Session

View source

Public Methods ​

MethodDescription
addFlash()Adds a flash message.
close()Closes the session, if open.
exists()Returns whether a PHP session exists (regardless of whether it’s currently active).
get()Returns the session variable value with the session variable name.
getAllFlashes()Returns all flash messages.
getFlash()Returns a flash message.
has()Returns whether a session variable exists.
hasFlash()Returns a value indicating whether there are flash messages associated with the specified key.
remove()Removes a session variable.
removeAll()Removes all session variables.
removeAllFlashes()Removes all flash messages.
removeFlash()Removes a flash message.
reset()Resets the memoized database connection.
set()Adds a session variable.

addFlash() ​

Since
4.13.0

Adds a flash message.

See also yii\web\Session::addFlash()View source

Arguments ​

  • $key (string) – The key identifying the flash message
  • $value (mixed) – Flash message
  • $removeAfterAccess (boolean) – Whether the flash message should be automatically removed only if it is accessed

close() ​

Since
4.12.0

Closes the session, if open.

View source

exists() ​

Returns whether a PHP session exists (regardless of whether it’s currently active).

View source

Returns ​

boolean

get() ​

Returns the session variable value with the session variable name.

View source

Arguments ​

  • $key (string) – The session variable name

Returns ​

mixed – The session variable value, or null if it doesn’t exist

getAllFlashes() ​

Since
4.13.0

Returns all flash messages.

See also yii\web\Session::getAllFlashes()View source

Arguments ​

  • $delete (boolean) – Whether to delete the flash messages right after this method is called

Returns ​

array – Flash messages (key => message or key => [message1, message2])

getFlash() ​

Since
4.13.0

Returns a flash message.

See also yii\web\Session::getFlash()View source

Arguments ​

  • $key (string) – The key identifying the flash message
  • $defaultValue (mixed) – Value to be returned if the flash message does not exist
  • $delete (boolean) – Whether to delete this flash message right after this method is called

Returns ​

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

has() ​

Returns whether a session variable exists.

View source

Arguments ​

  • $key (string) – The session variable name

Returns ​

boolean – Whether there is the named session variable

hasFlash() ​

Since
4.13.0

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

See also yii\web\Session::hasFlash()View source

Arguments ​

  • $key (string) – Key identifying the flash message type

Returns ​

boolean – Whether any flash messages exist under specified key

remove() ​

Removes a session variable.

View source

Arguments ​

  • $key (string) – The session variable name

Returns ​

mixed – The old value, or null if it didn’t exist

removeAll() ​

Removes all session variables.

View source

removeAllFlashes() ​

Since
4.13.0

Removes all flash messages.

See also yii\web\Session::removeAllFlashes()View source

removeFlash() ​

Since
4.13.0

Removes a flash message.

See also yii\web\Session::removeFlash()View source

Arguments ​

  • $key (string) – The key identifying the flash message

Returns ​

mixed – The removed flash message or null if the flash message does not exist

reset() ​

Since
3.5.12.1

Resets the memoized database connection.

View source

set() ​

Adds a session variable.

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

View source

Arguments ​

  • $key (string) – The session variable name
  • $value (mixed) – The session variable value