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
# 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]).
# 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
# hasFlash()
Returns a value indicating whether there are flash messages associated with the specified key.
View source (opens new window)
Arguments
$key
(string (opens new window)) – Key identifying the flash message type
Returns
boolean (opens new window) – Whether any flash messages exist under specified key
# 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. |