Session

Type
Class
Namespace
craft\web
Inherits
craft\web\Session » yii\web\Session (opens new window) » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
ArrayAccess (opens new window), Countable (opens new window), IteratorAggregate (opens new window), yii\base\Configurable (opens new window)
Since
3.0.0

Extends yii\web\Session (opens new window) to add support for setting the session folder and creating it if it doesn’t exist.

View source (opens new window)

# Public Properties

Property Description
allFlashes (opens new window) array (opens new window) – Flash messages (key => message or key => [message1, message2]).
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component.
cacheLimiter (opens new window) string (opens new window) – Current cache limiter.
cookieParams (opens new window) array (opens new window) – The session cookie parameters.
count (opens new window) integer (opens new window) – The number of session variables.
flash (opens new window) string (opens new window) – The key identifying the flash message.
flashParam (opens new window) string (opens new window) – The name of the session variable that stores the flash message data.
gCProbability (opens new window) float (opens new window) – The probability (percentage) that the GC (garbage collection) process is started on every session initialization.
handler (opens new window) SessionHandlerInterface (opens new window), array (opens new window) – An object implementing the SessionHandlerInterface or a configuration array.
hasSessionId (opens new window) boolean (opens new window) – Whether the current request has sent the session ID.
id (opens new window) string (opens new window) – The current session ID.
isActive (opens new window) boolean (opens new window) – Whether the session has started.
iterator (opens new window) yii\web\SessionIterator (opens new window) – An iterator for traversing the session variables.
name (opens new window) string (opens new window) – The current session name.
savePath (opens new window) string (opens new window) – The current session save path, defaults to '/tmp'.
timeout (opens new window) integer (opens new window) – The number of seconds after which data will be seen as 'garbage' and cleaned up.
useCookies (opens new window) boolean (opens new window), null (opens new window) – The value indicating whether cookies should be used to store session IDs.
useCustomStorage (opens new window) boolean (opens new window) – Whether to use custom storage.
useStrictMode (opens new window) boolean (opens new window) – Whether strict mode is enabled or not.
useTransparentSessionID (opens new window) boolean (opens new window) – Whether transparent sid support is enabled or not, defaults to false.

# Protected Properties

Property Description
_forceRegenerateId (opens new window) string (opens new window), null (opens new window) – Holds the session id in case useStrictMode is enabled and the session id needs to be regenerated
_originalSessionModule (opens new window) string (opens new window), null (opens new window) – 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.

# Public Methods

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

# getAllFlashes()

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 '<div class="alert alert-' . $key . '">' . $message . '</div>';
} ?>

With the above code you can use the bootstrap alert (opens new window) classes such as success, info, danger as the flash message key to influence the color of the div.

Note that if you use addFlash() (opens new window), $message will be an array, and you will have to adjust the above code.

View source (opens new window)

Arguments

  • $delete (boolean (opens new window)) – 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 (opens new window) – Flash messages (key => message or key => [message1, message2]).

Example

$messages = Craft::$app->session->getAllFlashes(true);

# getFlash()

Returns a flash message.

View source (opens new window)

Arguments

  • $key (string (opens new window)) – The key identifying the flash message
  • $defaultValue (mixed) – Value to be returned if the flash message does not exist.
  • $delete (boolean (opens new window)) – 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

Example

$message = Craft::$app->session->getFlash('notice', null, true);

# hasFlash()

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

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether any flash messages exist under specified key

Example

$hasNotice = Craft::$app->session->hasFlash('notice');

# Protected Methods

Method Description
freeze() (opens new window) If session is started it's not possible to edit session ini settings. In PHP7.2+ it throws exception.
registerSessionHandler() (opens new window) Registers session handler.
unfreeze() (opens new window) Starts session and restores data from temporary variable
updateFlashCounters() (opens new window) Updates the counters for flash messages and removes outdated flash messages.