class Session implements CookieEnabledInterface (View source)

A modular session implementation based on the caching framework.

You may access the currently active session in user land code. In order to do this, inject SessionInterface and NOT just the Session object. The former will be a unique instance (singleton) representing the current session while the latter would be a completely new session instance!

You can use the Session Manager for accessing sessions which are not currently active.

Note that Flow's bootstrap (that is, Neos\Flow\Core\Scripts) will try to resume a possibly existing session automatically. If a session could be resumed during that phase already, calling start() at a later stage will be a no-operation.

Constants

private FLOW_OBJECT_STORAGE_KEY

Properties

protected $objectManager
protected $logger
protected $sessionMetaDataStore
protected $sessionKeyValueStore
protected $sessionCookieName
protected $sessionCookieLifetime
protected $sessionCookieDomain
protected $sessionCookiePath
protected $sessionCookieSecure
protected $sessionCookieHttpOnly
protected $sessionCookieSameSite
protected $sessionCookie
protected $inactivityTimeout
protected $tags
protected $now
protected $sessionMetaData
protected $started
protected $remote

Methods

__construct()

Session instances MUST NOT be created manually! They should be retrieved via the Session Manager or through dependency injection (use SessionInterface!).

static Session
create()

No description

static Session
createRemote(string $sessionIdentifier, string $storageIdentifier, int $lastActivityTimestamp = null, array $tags)

No description

static Session
createRemoteFromSessionMetaData(SessionMetaData $sessionMetaData)

No description

static CookieEnabledInterface
createFromCookieAndSessionInformation(Cookie $sessionCookie, string $storageIdentifier, int $lastActivityTimestamp, array $tags = [])

No description

void
injectSettings(array $settings)

No description

getSessionCookie()

No description

bool
isStarted()

Tells if the session has been started already.

bool
isRemote()

Tells if the session is local (the current session bound to the current HTTP request) or remote (retrieved through the Session Manager).

void
start()

Starts the session, if it has not been already started

bool
canBeResumed()

Returns true if there is a session that can be resumed.

int|null
resume()

Resumes an existing session, if any.

string
getId()

Returns the current session identifier

string
renewId()

Generates and propagates a new session ID and transfers all existing data to the new session.

mixed
getData(string $key)

Returns the data associated with the given key.

bool
hasKey(string $key)

Returns true if a session data entry $key is available.

void
putData(string $key, mixed $data)

Stores the given data under the given key in the session

int
getLastActivityTimestamp()

Returns the unix time stamp marking the last point in time this session has been in use.

void
addTag(string $tag)

Tags this session with the given tag.

void
removeTag(string $tag)

Removes the specified tag from this session.

array
getTags()

Returns the tags this session has been tagged with.

void
touch()

Updates the last activity time to "now".

void
close()

Explicitly writes and closes the session

void
destroy(string|null $reason = null)

Explicitly destroys all session data

void
shutdownObject()

Shuts down this session

bool
autoExpire()

Automatically expires the session if the user has been inactive for too long.

void
writeSessionMetaDataCacheEntry()

Writes the cache entry containing information about the session, such as the last activity time and the storage identifier.

Details

__construct()

Session instances MUST NOT be created manually! They should be retrieved via the Session Manager or through dependency injection (use SessionInterface!).

static Session create()

No description

Return Value

Session

static Session createRemote(string $sessionIdentifier, string $storageIdentifier, int $lastActivityTimestamp = null, array $tags)

No description

Parameters

string $sessionIdentifier
string $storageIdentifier
int $lastActivityTimestamp
array $tags

Return Value

Session

static Session createRemoteFromSessionMetaData(SessionMetaData $sessionMetaData)

No description

Parameters

SessionMetaData $sessionMetaData

Return Value

Session

static CookieEnabledInterface createFromCookieAndSessionInformation(Cookie $sessionCookie, string $storageIdentifier, int $lastActivityTimestamp, array $tags = [])

No description

Parameters

Cookie $sessionCookie
string $storageIdentifier
int $lastActivityTimestamp
array $tags

Return Value

CookieEnabledInterface

void injectSettings(array $settings)

No description

Parameters

array $settings

Return Value

void

Cookie getSessionCookie()

No description

Return Value

Cookie

bool isStarted()

Tells if the session has been started already.

Return Value

bool

bool isRemote()

Tells if the session is local (the current session bound to the current HTTP request) or remote (retrieved through the Session Manager).

Return Value

bool

void start()

Starts the session, if it has not been already started

Return Value

void

Exceptions

Exception

See also

CookieEnabledInterface

bool canBeResumed()

Returns true if there is a session that can be resumed.

If a to-be-resumed session was inactive for too long, this function will trigger the expiration of that session. An expired session cannot be resumed.

NOTE that this method does a bit more than the name implies: Because the session info data needs to be loaded, this method stores this data already, so it doesn't have to be loaded again once the session is being used.

Return Value

bool

int|null resume()

Resumes an existing session, if any.

Return Value

int|null

If a session was resumed, the inactivity of this session since the last request is returned

Exceptions

InvalidDataException
Exception

string getId()

Returns the current session identifier

Return Value

string

Exceptions

SessionNotStartedException

string renewId()

Generates and propagates a new session ID and transfers all existing data to the new session.

Return Value

string

The new session ID

Exceptions

SessionNotStartedException
OperationNotSupportedException

mixed getData(string $key)

Returns the data associated with the given key.

Parameters

string $key

An identifier for the content stored in the session.

Return Value

mixed

The contents associated with the given key

Exceptions

SessionNotStartedException

bool hasKey(string $key)

Returns true if a session data entry $key is available.

Parameters

string $key

Return Value

bool

Exceptions

SessionNotStartedException

void putData(string $key, mixed $data)

Stores the given data under the given key in the session

Parameters

string $key

The key under which the data should be stored

mixed $data

The data to be stored

Return Value

void

Exceptions

DataNotSerializableException
SessionNotStartedException
Exception
InvalidDataException

int getLastActivityTimestamp()

Returns the unix time stamp marking the last point in time this session has been in use.

For the current (local) session, this method will always return the current time. For a remote session, the unix timestamp will be returned.

Return Value

int

unix timestamp

Exceptions

SessionNotStartedException

void addTag(string $tag)

Tags this session with the given tag.

Note that third-party libraries might also tag your session. Therefore it is recommended to use namespaced tags such as "Acme-Demo-MySpecialTag".

Parameters

string $tag

The tag – must match be a valid cache frontend tag

Return Value

void

Exceptions

SessionNotStartedException
InvalidArgumentException

void removeTag(string $tag)

Removes the specified tag from this session.

Parameters

string $tag

The tag – must match be a valid cache frontend tag

Return Value

void

Exceptions

SessionNotStartedException

array getTags()

Returns the tags this session has been tagged with.

Return Value

array

The tags or an empty array if there aren't any

Exceptions

SessionNotStartedException

void touch()

Updates the last activity time to "now".

Return Value

void

Exceptions

SessionNotStartedException

void close()

Explicitly writes and closes the session

Return Value

void

Exceptions

NotSupportedByBackendException
Exception

void destroy(string|null $reason = null)

Explicitly destroys all session data

Parameters

string|null $reason

A reason for destroying the session – used by the LoggingAspect

Return Value

void

Exceptions

SessionNotStartedException

void shutdownObject()

Shuts down this session

This method must not be called manually – it is invoked by Flow's object management.

Return Value

void

Exceptions

NotSupportedByBackendException
Exception

protected bool autoExpire()

Automatically expires the session if the user has been inactive for too long.

Return Value

bool

true if the session expired, false if not

protected void writeSessionMetaDataCacheEntry()

Writes the cache entry containing information about the session, such as the last activity time and the storage identifier.

This function does not write the whole session data into the storage cache, but only the "head" cache entry containing meta information.

The session cache entry is also tagged with "session", the session identifier and any custom tags of this session, prefixed with TAG_PREFIX.

Return Value

void

Exceptions

Exception