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 285
__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 299
bool open(string $savePath, string $sessionName)

Open the session.

Parameters

string $savePath
string $sessionName

Return Value

bool

at line 307
bool close()

Close the session.

Return Value

bool

at line 315
string create_sid()

Create a new session ID.

Return Value

string

at line 323
bool validateId(string $id)

Determine if the session ID exists.

Parameters

string $id

Return Value

bool

at line 333
string read(string $sessionId)

Read the session data.

Parameters

string $sessionId

Return Value

string

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

Write the session data.

Parameters

string $sessionId
string $data

Return Value

bool

at line 426
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 494
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 526
bool destroy(string $sessionId)

Destroy the given session.

Parameters

string $sessionId

Return Value

bool

at line 570
int gc(int $lifetime)

Perform session garbage collection.

Parameters

int $lifetime

Return Value

int

at line 578
bool supportsUserSessionManagement()

Determine if the handler supports user session management.

Return Value

bool

at line 588
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 647
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 692
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 725
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 794
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 811
protected string payloadKey(string $sessionId)

Get a payload key.

Parameters

string $sessionId

Return Value

string

at line 819
protected string userIndexKey(string $owner)

Get a user session index key.

Parameters

string $owner

Return Value

string

at line 827
protected string physicalPrefix(RedisConnection $connection)

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

Parameters

RedisConnection $connection

Return Value

string

at line 835
protected int lifetimeInSeconds()

Get the configured session lifetime in seconds.

Return Value

int

at line 843
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 857
protected string ownerFromStoredValue(string $value)

Get the proven owner from a stored session value.

Parameters

string $value

Return Value

string

at line 872
protected string payloadFromStoredValue(string $value)

Strip a valid ownership envelope from a stored session payload.

Parameters

string $value

Return Value

string

at line 888
protected bool validOwnerDigest(string $owner)

Determine if a value is a valid owner digest.

Parameters

string $owner

Return Value

bool

at line 896
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 913
protected string|null observedOwner(string $sessionId)

Get an owner observed for a Redis Cluster session.

Parameters

string $sessionId

Return Value

string|null

at line 923
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 936
protected array ownerObservations()

Get this handler's Redis Cluster owner observations.

Return Value

array

at line 944
protected void forgetOwnerObservations()

Forget this handler's Redis Cluster owner observations.

Return Value

void

at line 952
protected string ownerContextKey()

Get this handler's Redis Cluster owner context key.

Return Value

string

at line 962
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 976
protected array|null decodeMetadata(string $value)

Decode and validate tracked session metadata.

Parameters

string $value

Return Value

array|null

at line 1005
protected string|null ipAddress()

Get the current request IP address.

Return Value

string|null

at line 1013
protected string userAgent()

Get the current request user agent.

Return Value

string

at line 1030
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 1052
protected array parseDestroyResult(mixed $value)

Parse a cluster payload deletion result.

Parameters

mixed $value

Return Value

array

at line 1071
protected mixed withConnection(Closure $callback)

Execute an operation on a pinned raw Redis connection.

Parameters

Closure $callback

Return Value

mixed

at line 1090
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