DatabaseHandler class

Framework\Session\SaveHandlers\DatabaseHandler

Class DatabaseHandler.

```sql
CREATE TABLE `Sessions` (
`id` varchar(128) NOT NULL,
`timestamp` timestamp NOT NULL,
`data` blob NOT NULL,
`ip` varchar(45) NOT NULL, -- optional
`ua` varchar(255) NOT NULL, -- optional
PRIMARY KEY (`id`),
KEY `timestamp` (`timestamp`),
KEY `ip` (`ip`), -- optional
KEY `ua` (`ua`) -- optional
);
```

NOTE: As of PHP 8.4 the id column can be `char(32)`.

Extends Framework\Session\SaveHandler

Implements: SessionUpdateTimestampHandlerInterface, SessionHandlerInterface

Defined in libraries/session/src/SaveHandlers/DatabaseHandler.php

Properties #

$database
-

$database

protected ?\Framework\Database\Database $database

Methods #

close()
-
destroy()
-
gc()
-
getDatabase()
-
open()
-
read()
-
setDatabase()
-
updateTimestamp()
-
write()
-
addUserIdColumn()
Adds the optional `user_id` column.
addWhereMatchs()
Adds the `WHERE $column = $value` clauses when matching IP or User-Agent.
getColumn()
Get a column name based on custom/default configs.
getTable()
Get the table name based on custom/default configs.
lock()
-
prepareConfig()
Prepare configurations to be used by the DatabaseHandler.
unlock()
-
writeInsert()
-
writeUpdate()
-

close()

public function close(): bool
Returns bool

destroy()

public function destroy($id): bool
Parameters
  • $id
Returns bool

gc()

public function gc($max_lifetime): int|false
Parameters
  • $max_lifetime
Returns int|false

getDatabase()

public function getDatabase(): ?\Framework\Database\Database
Returns ?\Framework\Database\Database

open()

public function open($path, $name): bool
Parameters
  • $path
  • $name
Returns bool

read()

public function read($id): string
Parameters
  • $id
Returns string

setDatabase()

public function setDatabase(\Framework\Database\Database $database): static
Parameters
  • \Framework\Database\Database $database
Returns static

updateTimestamp()

public function updateTimestamp($id, $data): bool
Parameters
  • $id
  • $data
Returns bool

write()

public function write($id, $data): bool
Parameters
  • $id
  • $data
Returns bool

addUserIdColumn()

protected function addUserIdColumn(array &$columns): void

Adds the optional `user_id` column.

Parameters
  • array $columns - The statement columns to insert/update
Returns void

addWhereMatchs()

protected function addWhereMatchs(\Framework\Database\Manipulation\Delete|\Framework\Database\Manipulation\Select|\Framework\Database\Manipulation\Update $statement): void

Adds the `WHERE $column = $value` clauses when matching IP or User-Agent.

Parameters
  • \Framework\Database\Manipulation\Delete|\Framework\Database\Manipulation\Select|\Framework\Database\Manipulation\Update $statement - The statement to add the WHERE clause
Returns void

getColumn()

protected function getColumn(string $key): string

Get a column name based on custom/default configs.

Parameters
  • string $key - The columns config key
Returns string - The column name

getTable()

protected function getTable(): string

Get the table name based on custom/default configs.

Returns string - The table name

lock()

protected function lock(string $id): bool
Parameters
  • string $id
Returns bool

prepareConfig()

protected function prepareConfig(array $config): void

Prepare configurations to be used by the DatabaseHandler.

Parameters
  • array $config - Custom configs The custom configs are: ```php $configs = [ // The name of the table used for sessions 'table' => 'Sessions', // The maxlifetime used for locking 'maxlifetime' => null, // Null to use the ini value of session.gc_maxlifetime // The custom column names as values 'columns' => [ 'id' => 'id', 'data' => 'data', 'timestamp' => 'timestamp', 'ip' => 'ip', 'ua' => 'ua', ], // Match IP? 'match_ip' => false, // Match User-Agent? 'match_ua' => false, // Independent of match_ip, save the initial IP in the ip column? 'save_ip' => false, // Independent of match_ua, save the initial User-Agent in the ua column? 'save_ua' => false, ]; ``` NOTE: The Database::connect configs was not shown.
Returns void

unlock()

protected function unlock(): bool
Returns bool

writeInsert()

protected function writeInsert(string $id, string $data): bool
Parameters
  • string $id
  • string $data
Returns bool

writeUpdate()

protected function writeUpdate(string $id, string $data): bool
Parameters
  • string $id
  • string $data
Returns bool

Inherited methods #