UserSession
Represents all information about a user's session.
A user session can be bound to a frontend / backend user, or an anonymous session based on session data stored in the session backend.
If a session is anonymous, it can be fixated by storing the session in the backend, but only if there is data in the session.
if a session is user-bound, it is automatically fixated.
The $isNew flag is meant to show that this user session object was not fetched from the session backend, but initialized in the first place by the current request.
The $data argument is to store any arbitrary data valid for the users' session.
A permanent session means that the client is not issued a session-based cookie but a time-based cookie. So the server-session survives the session of the browser.
Table of Contents
Constants
Properties
- $data : array<string|int, mixed>
- $identifier : string
- $ipLock : string
- $isNew : bool
- $isPermanent : bool
- $lastUpdated : int
- $userId : int|null
- $wasUpdated : bool
Methods
- createFromRecord() : UserSession
- Create a new user session based on the provided session record
- createNonFixated() : UserSession
- Create a non fixated user session. This means the session does not belong to a logged-in user.
- dataWasUpdated() : bool
- Check if session data was already updated
- get() : mixed
- Return the data for the given key or an NULL if the key does not exist
- getData() : array<string|int, mixed>
- Return the whole session data array.
- getIdentifier() : string
- Get the user session identifier (the ses_id)
- getIpLock() : string
- Return the sessions ipLock state
- getLastUpdated() : int
- Get the timestamp of the last session data update
- getUserId() : int
- Get the user id (ID of the user record to whom the session belongs)
- hasData() : bool
- Check whether the session has data
- isAnonymous() : bool
- Check if the user session is an anonymous one.
- isNew() : bool
- Check whether the session was marked as new on creation
- isPermanent() : bool
- Check whether the session was marked as permanent on creation
- needsUpdate() : bool
- Use a gracetime-value to avoid updating a session-record too often
- overrideData() : void
- Override the whole $data. Can be used to e.g. preserve session data on login or to remove session data by providing an empty array.
- set() : void
- Set / update a data value for a given key.
- toArray() : array<string|int, mixed>
- Used internally to store data in the backend
- __construct() : mixed
Constants
SESSION_UPDATE_GRACE_PERIOD
protected
mixed
SESSION_UPDATE_GRACE_PERIOD
= 61
Properties
$data
protected
array<string|int, mixed>
$data
$identifier
protected
string
$identifier
$ipLock
protected
string
$ipLock
= ''
$isNew
protected
bool
$isNew
= true
$isPermanent
protected
bool
$isPermanent
= false
$lastUpdated
protected
int
$lastUpdated
$userId
protected
int|null
$userId
$wasUpdated
protected
bool
$wasUpdated
= false
Methods
createFromRecord()
Create a new user session based on the provided session record
public
static createFromRecord(string $id, array<string|int, mixed> $record[, bool $markAsNew = false ]) : UserSession
Parameters
- $id : string
-
the session identifier
- $record : array<string|int, mixed>
- $markAsNew : bool = false
Return values
UserSessioncreateNonFixated()
Create a non fixated user session. This means the session does not belong to a logged-in user.
public
static createNonFixated(string $identifier) : UserSession
Parameters
- $identifier : string
Return values
UserSessiondataWasUpdated()
Check if session data was already updated
public
dataWasUpdated() : bool
Return values
boolget()
Return the data for the given key or an NULL if the key does not exist
public
get(string $key) : mixed
Parameters
- $key : string
getData()
Return the whole session data array.
public
getData() : array<string|int, mixed>
Return values
array<string|int, mixed>getIdentifier()
Get the user session identifier (the ses_id)
public
getIdentifier() : string
Return values
stringgetIpLock()
Return the sessions ipLock state
public
getIpLock() : string
Return values
stringgetLastUpdated()
Get the timestamp of the last session data update
public
getLastUpdated() : int
Return values
intgetUserId()
Get the user id (ID of the user record to whom the session belongs)
public
getUserId() : int
Return values
inthasData()
Check whether the session has data
public
hasData() : bool
Return values
boolisAnonymous()
Check if the user session is an anonymous one.
public
isAnonymous() : bool
This means, the session does not belong to a logged-in user.
Return values
boolisNew()
Check whether the session was marked as new on creation
public
isNew() : bool
Return values
boolisPermanent()
Check whether the session was marked as permanent on creation
public
isPermanent() : bool
Return values
boolneedsUpdate()
Use a gracetime-value to avoid updating a session-record too often
public
needsUpdate() : bool
Return values
booloverrideData()
Override the whole $data. Can be used to e.g. preserve session data on login or to remove session data by providing an empty array.
public
overrideData(array<string|int, mixed> $data) : void
Parameters
- $data : array<string|int, mixed>
set()
Set / update a data value for a given key.
public
set(string $key, mixed $value) : void
Throws an exception if the given key is empty.
Parameters
- $key : string
-
The key whose value should be updated
- $value : mixed
-
The value or NULL to unset the key
toArray()
Used internally to store data in the backend
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed> —The session record as array
__construct()
protected
__construct(string $identifier, int $userId, int $lastUpdated[, array<string|int, mixed> $data = [] ]) : mixed
Parameters
- $identifier : string
- $userId : int
- $lastUpdated : int
- $data : array<string|int, mixed> = []