class RedisSessionHandler implements CanManageUserSessions, SessionHandlerInterface

Traits

Constants

protected ENVELOPE_FAMILY

Envelope values mirrored by the Lua ownership parser below.

protected ENVELOPE_VERSION

protected OWNER_DIGEST_LENGTH

protected ENVELOPE_HEADER_LENGTH

protected USER_INDEX_PREFIX

User-index fragments mirrored where Lua constructs dynamic owner keys.

protected USER_INDEX_SUFFIX

protected REDIS_OWNER_CONTEXT_KEY_PREFIX

Context key prefix for owners observed from Redis Cluster payloads.

protected LUA_OWNER_OF

protected LUA_VALID_METADATA

protected WRITE_SCRIPT

protected DESTROY_SCRIPT

protected DESTROY_USER_SESSION_SCRIPT

protected DESTROY_USER_SESSIONS_SCRIPT

protected CLUSTER_WRITE_SCRIPT

protected CLUSTER_UPDATE_INDEX_SCRIPT

protected CLUSTER_DESTROY_SCRIPT

Methods

int
secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

int
availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)

Get the "available at" UNIX timestamp.

parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)

If the given value is an interval, convert it to a DateTime instance.

int
currentTime()

Get the current system time as a UNIX timestamp.

string
runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

void
validateAuthProvider(string $authProvider)

Validate an authentication provider supplied to the handler.

array
normalizeSessionIds(array $sessionIds)

Validate and deduplicate session identifiers supplied to the handler.

__construct(Factory $redis, string $connection, string $prefix, int $minutes, bool $trackUserSessions = false, Container|null $container = null)

Create a new Redis session handler instance.

bool
open(string $savePath, string $sessionName)

Open the session.

bool
close()

Close the session.

string
create_sid()

Create a new session ID.

bool
validateId(string $id)

Determine if the session ID exists.

string
read(string $sessionId)

Read the session data.

bool
write(string $sessionId, string $data)

Write the session data.

bool
writeCluster(RedisConnection $connection, string $sessionId, string $data, string $identityState, string $resolvedOwner, int $lastActivity, int $expiresAt, bool $hasFreshMetadata, string $metadata)

Write a tracked session to Redis Cluster.

bool
updateClusterIndex(RedisConnection $connection, string $owner, string $sessionId, int $expiresAt, int $lastActivity, bool $hasFreshMetadata, string $metadata)

Update a session field in a Redis Cluster user index.

bool
destroy(string $sessionId)

Destroy the given session.

int
gc(int $lifetime)

Perform session garbage collection.

bool
supportsUserSessionManagement()

Determine if the handler supports user session management.

userSessions(string $authProvider, int|string $userId)

Get the active sessions for the given user.

bool
destroyUserSession(string $authProvider, int|string $userId, string $sessionId)

Destroy an active session belonging to the given user.

int
destroyUserSessions(string $authProvider, int|string $userId, array $except = [])

Destroy the active sessions belonging to the given user.

int
destroyUserSessionsInCluster(RedisConnection $connection, string $owner, array $except)

Destroy user sessions across Redis Cluster slots.

void
cleanUserIndex(RedisConnection $connection, string $owner, array $sessionIds)

Clean fields from a user's session index.

string
payloadKey(string $sessionId)

Get a payload key.

string
userIndexKey(string $owner)

Get a user session index key.

string
physicalPrefix(RedisConnection $connection)

Get the full physical prefix used for dynamically constructed Lua keys.

int
lifetimeInSeconds()

Get the configured session lifetime in seconds.

string
ownerDigest(string $authProvider, string $userId)

Hash a provider-qualified user identifier for use in Redis keys and envelopes.

string
ownerFromStoredValue(string $value)

Get the proven owner from a stored session value.

string
payloadFromStoredValue(string $value)

Strip a valid ownership envelope from a stored session payload.

bool
validOwnerDigest(string $owner)

Determine if a value is a valid owner digest.

string
probeOwner(RedisConnection $connection, string $sessionId)

Probe and remember the owner of a Redis Cluster session payload.

string|null
observedOwner(string $sessionId)

Get an owner observed for a Redis Cluster session.

void
rememberOwner(string $sessionId, string $owner)

Remember a Redis Cluster session owner for the current coroutine.

array
ownerObservations()

Get this handler's Redis Cluster owner observations.

void
forgetOwnerObservations()

Forget this handler's Redis Cluster owner observations.

string
ownerContextKey()

Get this handler's Redis Cluster owner context key.

string
encodeMetadata(int $lastActivity, bool $hasFreshMetadata)

Encode metadata for a tracked session write.

array|null
decodeMetadata(string $value)

Decode and validate tracked session metadata.

string|null
ipAddress()

Get the current request IP address.

string
userAgent()

Get the current request user agent.

array
parseOwnerTransition(mixed $value)

Parse the ownership transition returned by the cluster write script.

array
parseDestroyResult(mixed $value)

Parse a cluster payload deletion result.

mixed
withConnection(Closure $callback)

Execute an operation on a pinned raw Redis connection.

void
__clone()

Reset this handler's observed owners when it is cloned.

Details

in InteractsWithTime at line 17
protected int secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

Parameters

DateInterval|DateTimeInterface|int $delay

Return Value

int

in InteractsWithTime at line 29
protected int availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)

Get the "available at" UNIX timestamp.

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

int

in InteractsWithTime at line 47
protected DateTimeInterface|int parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)

If the given value is an interval, convert it to a DateTime instance.

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

DateTimeInterface|int

in InteractsWithTime at line 63
protected int currentTime()

Get the current system time as a UNIX timestamp.

Return Value

int

in InteractsWithTime at line 71
protected string runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

Parameters

float $startTime
float|null $endTime

Return Value

string

protected void validateAuthProvider(string $authProvider)

Validate an authentication provider supplied to the handler.

Parameters

string $authProvider

Return Value

void

protected array normalizeSessionIds(array $sessionIds)

Validate and deduplicate session identifiers supplied to the handler.

Parameters

array $sessionIds

Return Value

array

at line 286
__construct(Factory $redis, string $connection, string $prefix, int $minutes, bool $trackUserSessions = false, Container|null $container = null)

Create a new Redis session handler instance.

Parameters

Factory $redis
string $connection
string $prefix
int $minutes
bool $trackUserSessions
Container|null $container

at line 300
bool open(string $savePath, string $sessionName)

Open the session.

Parameters

string $savePath
string $sessionName

Return Value

bool

at line 308
bool close()

Close the session.

Return Value

bool

at line 316
string create_sid()

Create a new session ID.

Return Value

string

at line 324
bool validateId(string $id)

Determine if the session ID exists.

Parameters

string $id

Return Value

bool

at line 334
string read(string $sessionId)

Read the session data.

Parameters

string $sessionId

Return Value

string

at line 357
bool write(string $sessionId, string $data)

Write the session data.

Parameters

string $sessionId
string $data

Return Value

bool

at line 427
protected bool writeCluster(RedisConnection $connection, string $sessionId, string $data, string $identityState, string $resolvedOwner, int $lastActivity, int $expiresAt, bool $hasFreshMetadata, string $metadata)

Write a tracked session to Redis Cluster.

Parameters

RedisConnection $connection
string $sessionId
string $data
string $identityState
string $resolvedOwner
int $lastActivity
int $expiresAt
bool $hasFreshMetadata
string $metadata

Return Value

bool

at line 495
protected bool updateClusterIndex(RedisConnection $connection, string $owner, string $sessionId, int $expiresAt, int $lastActivity, bool $hasFreshMetadata, string $metadata)

Update a session field in a Redis Cluster user index.

Parameters

RedisConnection $connection
string $owner
string $sessionId
int $expiresAt
int $lastActivity
bool $hasFreshMetadata
string $metadata

Return Value

bool

at line 527
bool destroy(string $sessionId)

Destroy the given session.

Parameters

string $sessionId

Return Value

bool

at line 571
int gc(int $lifetime)

Perform session garbage collection.

Parameters

int $lifetime

Return Value

int

at line 579
bool supportsUserSessionManagement()

Determine if the handler supports user session management.

Return Value

bool

at line 589
Collection userSessions(string $authProvider, int|string $userId)

Get the active sessions for the given user.

Parameters

string $authProvider
int|string $userId

Return Value

Collection

at line 648
bool destroyUserSession(string $authProvider, int|string $userId, string $sessionId)

Destroy an active session belonging to the given user.

Parameters

string $authProvider
int|string $userId
string $sessionId

Return Value

bool

at line 693
int destroyUserSessions(string $authProvider, int|string $userId, array $except = [])

Destroy the active sessions belonging to the given user.

Parameters

string $authProvider
int|string $userId
array $except

Return Value

int

at line 726
protected int destroyUserSessionsInCluster(RedisConnection $connection, string $owner, array $except)

Destroy user sessions across Redis Cluster slots.

Parameters

RedisConnection $connection
string $owner
array $except

Return Value

int

at line 799
protected void cleanUserIndex(RedisConnection $connection, string $owner, array $sessionIds)

Clean fields from a user's session index.

Parameters

RedisConnection $connection
string $owner
array $sessionIds

Return Value

void

at line 816
protected string payloadKey(string $sessionId)

Get a payload key.

Parameters

string $sessionId

Return Value

string

at line 824
protected string userIndexKey(string $owner)

Get a user session index key.

Parameters

string $owner

Return Value

string

at line 832
protected string physicalPrefix(RedisConnection $connection)

Get the full physical prefix used for dynamically constructed Lua keys.

Parameters

RedisConnection $connection

Return Value

string

at line 840
protected int lifetimeInSeconds()

Get the configured session lifetime in seconds.

Return Value

int

at line 848
protected string ownerDigest(string $authProvider, string $userId)

Hash a provider-qualified user identifier for use in Redis keys and envelopes.

Parameters

string $authProvider
string $userId

Return Value

string

at line 862
protected string ownerFromStoredValue(string $value)

Get the proven owner from a stored session value.

Parameters

string $value

Return Value

string

at line 877
protected string payloadFromStoredValue(string $value)

Strip a valid ownership envelope from a stored session payload.

Parameters

string $value

Return Value

string

at line 893
protected bool validOwnerDigest(string $owner)

Determine if a value is a valid owner digest.

Parameters

string $owner

Return Value

bool

at line 901
protected string probeOwner(RedisConnection $connection, string $sessionId)

Probe and remember the owner of a Redis Cluster session payload.

Parameters

RedisConnection $connection
string $sessionId

Return Value

string

at line 918
protected string|null observedOwner(string $sessionId)

Get an owner observed for a Redis Cluster session.

Parameters

string $sessionId

Return Value

string|null

at line 928
protected void rememberOwner(string $sessionId, string $owner)

Remember a Redis Cluster session owner for the current coroutine.

Parameters

string $sessionId
string $owner

Return Value

void

at line 941
protected array ownerObservations()

Get this handler's Redis Cluster owner observations.

Return Value

array

at line 949
protected void forgetOwnerObservations()

Forget this handler's Redis Cluster owner observations.

Return Value

void

at line 957
protected string ownerContextKey()

Get this handler's Redis Cluster owner context key.

Return Value

string

at line 967
protected string encodeMetadata(int $lastActivity, bool $hasFreshMetadata)

Encode metadata for a tracked session write.

Parameters

int $lastActivity
bool $hasFreshMetadata

Return Value

string

Exceptions

JsonException

at line 981
protected array|null decodeMetadata(string $value)

Decode and validate tracked session metadata.

Parameters

string $value

Return Value

array|null

at line 1010
protected string|null ipAddress()

Get the current request IP address.

Return Value

string|null

at line 1018
protected string userAgent()

Get the current request user agent.

Return Value

string

at line 1035
protected array parseOwnerTransition(mixed $value)

Parse the ownership transition returned by the cluster write script.

A status of zero means the second element is the actual current owner and the third element is empty because no payload mutation occurred.

Parameters

mixed $value

Return Value

array

at line 1057
protected array parseDestroyResult(mixed $value)

Parse a cluster payload deletion result.

Parameters

mixed $value

Return Value

array

at line 1076
protected mixed withConnection(Closure $callback)

Execute an operation on a pinned raw Redis connection.

Parameters

Closure $callback

Return Value

mixed

at line 1095
void __clone()

Reset this handler's observed owners when it is cloned.

PHP reuses freed object IDs, so a clone can land on a released handler's ID and must not inherit its owner observations.

Return Value

void