SDK for PHP V3

SessionHandler
in package
implements SessionHandlerInterface

Provides an interface for using Amazon DynamoDB as a session store by hooking into PHP's session handler hooks. Once registered, You may use the native `$_SESSION` superglobal and session functions, and the sessions will be stored automatically in DynamoDB. DynamoDB is a great session storage solution due to its speed, scalability, and fault tolerance.

For maximum performance, we recommend that you keep the size of your sessions small. Locking is disabled by default, since it can drive up latencies and costs under high traffic. Only turn it on if you need it.

By far, the most expensive operation is garbage collection. Therefore, we encourage you to carefully consider your session garbage collection strategy. Note: the DynamoDB Session Handler does not allow garbage collection to be triggered randomly. You must run garbage collection manually or through other automated means using a cron job or similar scheduling technique.

Table of Contents

Interfaces

SessionHandlerInterface

Methods

__construct()  : mixed
close()  : bool
Close a session from writing.
create_sid()  : string|false
Create a new session ID. Triggered by session_start(), session_regenerate_id() and session_create_id().
destroy()  : bool
Delete a session stored in DynamoDB.
fromClient()  : SessionHandler
Creates a new DynamoDB Session Handler.
garbageCollect()  : mixed
Triggers garbage collection on expired sessions.
gc()  : bool
Satisfies the session handler interface, but does nothing. To do garbage collection, you must manually call the garbageCollect() method.
open()  : bool
Open a session for writing. Triggered by session_start().
read()  : string
Read a session stored in DynamoDB.
register()  : bool
Register the DynamoDB session handler.
validateId()  : bool
Check whether a session ID is in use. Triggered by session_start() when session.use_strict_mode is enabled, and after create_sid() so PHP does not hand out an ID that is already taken.
write()  : bool
Write a session to DynamoDB.

Methods

close()

Close a session from writing.

public close() : bool
Return values
bool —

Success

create_sid()

Create a new session ID. Triggered by session_start(), session_regenerate_id() and session_create_id().

public create_sid() : string|false

Uses PHP's own session ID generator, the same one PHP uses when a handler does not define this method.

Return values
string|false —

The new session ID.

destroy()

Delete a session stored in DynamoDB.

public destroy(string $id) : bool
Parameters
$id : string

Session ID.

Return values
bool —

Whether or not the operation succeeded.

fromClient()

Creates a new DynamoDB Session Handler.

public static fromClient(DynamoDbClient $client[, array<string|int, mixed> $config = [] ]) : SessionHandler

The configuration array accepts the following array keys and values:

  • table_name: Name of table to store the sessions.
  • hash_key: Name of hash key in table. Default: "id".
  • data_attribute: Name of the data attribute in table. Default: "data".
  • session_lifetime: Lifetime of inactive sessions expiration.
  • session_lifetime_attribute: Name of the session life time attribute in table. Default: "expires".
  • consistent_read: Whether or not to use consistent reads.
  • batch_config: Batch options used for garbage collection.
  • locking: Whether or not to use session locking.
  • max_lock_wait_time: Max time (s) to wait for lock acquisition.
  • min_lock_retry_microtime: Min time (µs) to wait between lock attempts.
  • max_lock_retry_microtime: Max time (µs) to wait between lock attempts.

You can find the full list of parameters and defaults within the trait Aws\DynamoDb\SessionConnectionConfigTrait

Parameters
$client : DynamoDbClient

Client for doing DynamoDB operations

$config : array<string|int, mixed> = []

Configuration for the Session Handler

Return values
SessionHandler

garbageCollect()

Triggers garbage collection on expired sessions.

public garbageCollect() : mixed
Tags
codeCoverageIgnore

gc()

Satisfies the session handler interface, but does nothing. To do garbage collection, you must manually call the garbageCollect() method.

public gc(int $maxLifetime) : bool
Parameters
$maxLifetime : int

Ignored.

Tags
codeCoverageIgnore
Return values
bool —

Whether or not the operation succeeded.

open()

Open a session for writing. Triggered by session_start().

public open(string $savePath, string $sessionName) : bool
Parameters
$savePath : string

Session save path.

$sessionName : string

Session name.

Return values
bool —

Whether or not the operation succeeded.

read()

Read a session stored in DynamoDB.

public read(string $id) : string
Parameters
$id : string

Session ID.

Return values
string —

Session data.

register()

Register the DynamoDB session handler.

public register() : bool
Tags
codeCoverageIgnore
Return values
bool —

Whether or not the handler was registered.

validateId()

Check whether a session ID is in use. Triggered by session_start() when session.use_strict_mode is enabled, and after create_sid() so PHP does not hand out an ID that is already taken.

public validateId(string $id) : bool

The lookup is a strongly consistent GetItem that never acquires the session lock, also when locking is enabled. With locking, an item that holds the lock counts as in use, even before its first write.

Connections that do not extend StandardSessionConnection cannot be checked, so they accept every ID PHP received, as PHP does when a handler does not define this method. The same applies when the lookup fails, leaving the error to read() as before.

Parameters
$id : string

Session ID.

Return values
bool —

Whether the session exists and has not expired.

write()

Write a session to DynamoDB.

public write(string $id, string $data) : bool
Parameters
$id : string

Session ID.

$data : string

Serialized session data to write.

Return values
bool —

Whether or not the operation succeeded.

<-- modeled_exceptions -->
On this page