SaveHandler class abstract

Framework\Session\SaveHandler

Class SaveHandler.

Implements: SessionHandlerInterface, SessionUpdateTimestampHandlerInterface

Defined in libraries/session/src/SaveHandler.php

Properties #

$config
The configurations used by the save handler.
$fingerprint
The current data fingerprint.
$lockId
The lock id or false if is not locked.
$sessionExists
Tells if the session exists (if was read).
$sessionId
The current session ID.
$logger
The Logger instance or null if it was not set.
$setByExternal
It says that the handler object was set with an externally created object.

$config

protected array $config

The configurations used by the save handler.

$fingerprint

protected string $fingerprint

The current data fingerprint.

$lockId

protected string|false $lockId = false

The lock id or false if is not locked.

$sessionExists

protected bool $sessionExists = false

Tells if the session exists (if was read).

$sessionId

protected ?string $sessionId

The current session ID.

$logger

protected ?\Framework\Log\Logger $logger

The Logger instance or null if it was not set.

$setByExternal

protected bool $setByExternal = false

It says that the handler object was set with an externally created object.

Methods #

__construct()
SessionSaveHandler constructor.
close()
Close the session.
destroy()
Destroy a session.
gc()
Cleanup old sessions.
getConfig()
-
open()
Initialize the session.
read()
Read session data.
updateTimestamp()
Update the timestamp of a session.
validateId()
Validate session id.
write()
Write session data.
getIP()
Get the remote IP address.
getKeySuffix()
-
getMaxlifetime()
Get the maxlifetime (TTL) used by cache handlers or locking.
getUA()
Get the HTTP User-Agent.
hasSameFingerprint()
Tells if the data has the same current fingerprint.
lock()
Acquire a lock for a session id.
log()
Log a message if the Logger is set.
prepareConfig()
Prepare configurations to be used by the save handler.
setFingerprint()
Set the data fingerprint.
unlock()
Unlock the current session lock id.
makeFingerprint()
Make the fingerprint value.

__construct()

public function __construct(array $config = array ( ), ?\Framework\Log\Logger $logger = NULL)

SessionSaveHandler constructor.

Parameters
  • array $config
  • ?\Framework\Log\Logger $logger

close()

public abstract function close(): bool

Close the session.

Returns bool - Returns TRUE on success, FALSE on failure

destroy()

public abstract function destroy($id): bool

Destroy a session.

Parameters
  • string $id - The session ID being destroyed
Returns bool - Returns TRUE on success, FALSE on failure

gc()

public abstract function gc($max_lifetime): int|false

Cleanup old sessions.

Parameters
  • int $max_lifetime - Sessions that have not updated for the last $maxLifetime seconds will be removed
Returns int|false - Returns the number of deleted session data for success, false for failure

getConfig()

public function getConfig(): array
Returns array

open()

public abstract function open($path, $name): bool

Initialize the session.

Parameters
  • string $path - The path where to store/retrieve the session
  • string $name - The session name
Returns bool - Returns TRUE on success, FALSE on failure

read()

public abstract function read($id): string

Read session data.

Parameters
  • string $id - The session id to read data for
Returns string - Returns an encoded string of the read data. If nothing was read, it returns an empty string

updateTimestamp()

public abstract function updateTimestamp($id, $data): bool

Update the timestamp of a session.

Parameters
  • string $id - The session id
  • string $data - The encoded session data. This data is the result of the PHP internally encoding the $_SESSION superglobal to a serialized string and passing it as this parameter. NOTE: Sessions can use an alternative serialization method
Returns bool - Returns TRUE on success, FALSE on failure

validateId()

public function validateId($id): bool

Validate session id.

Parameters
  • string $id - The session id
Returns bool - Returns TRUE if the id is valid, otherwise FALSE

write()

public abstract function write($id, $data): bool

Write session data.

Parameters
  • string $id - The session id
  • string $data - The encoded session data. This data is the result of the PHP internally encoding the $_SESSION superglobal to a serialized string and passing it as this parameter. NOTE: Sessions can use an alternative serialization method
Returns bool - Returns TRUE on success, FALSE on failure

getIP()

protected function getIP(): string

Get the remote IP address.

Returns string

getKeySuffix()

protected function getKeySuffix(): string
Returns string

getMaxlifetime()

protected function getMaxlifetime(): int

Get the maxlifetime (TTL) used by cache handlers or locking.

NOTE: It will use the `maxlifetime` config or the ini value of
`session.gc_maxlifetime` as fallback.

Returns int - The maximum lifetime of a session in seconds

getUA()

protected function getUA(): string

Get the HTTP User-Agent.

Returns string

hasSameFingerprint()

protected function hasSameFingerprint(string $data): bool

Tells if the data has the same current fingerprint.

Parameters
  • string $data - The data to compare
Returns bool - True if the fingerprints are the same, otherwise false

lock()

protected abstract function lock(string $id): bool

Acquire a lock for a session id.

Parameters
  • string $id - The session id
Returns bool - Returns TRUE on success, FALSE on failure

log()

protected function log(string $message, \Framework\Log\LogLevel $level = \Framework\Log\LogLevel::ERROR): void

Log a message if the Logger is set.

Parameters
  • string $message - The message to log
  • \Framework\Log\LogLevel $level - The log level
Returns void

prepareConfig()

protected function prepareConfig(array $config): void

Prepare configurations to be used by the save handler.

Parameters
  • array $config - Custom configs
Returns void

setFingerprint()

protected function setFingerprint(string $data): void

Set the data fingerprint.

Parameters
  • string $data - The data to set the new fingerprint
Returns void

unlock()

protected abstract function unlock(): bool

Unlock the current session lock id.

Returns bool - Returns TRUE on success, FALSE on failure

makeFingerprint()

private function makeFingerprint(string $data): string

Make the fingerprint value.

Parameters
  • string $data - The data to get the fingerprint
Returns string - The fingerprint hash