Session class

Framework\Session\Session

Class Session.

Defined in libraries/session/src/Session.php

Properties #

$options
-
$saveHandler
-
$debugCollector
-

$options

protected array $options = array ( )

$saveHandler

protected \Framework\Session\SaveHandler $saveHandler

$debugCollector

protected \Framework\Session\Debug\SessionCollector $debugCollector

Methods #

__construct()
Session constructor.
__destruct()
-
__get()
-
__isset()
-
__set()
-
__unset()
-
abort()
Discard session data changes and end session.
activate()
Make sure the session is active.
destroy()
Destroys all data registered to a session.
destroyCookie()
Sets a Cookie with the session name to be destroyed in the user-agent.
gc()
Perform session data garbage collection.
get()
Gets one session item.
getAll()
Get all session items.
getFlash()
Get a Flash Data item.
getMulti()
Get multiple session items.
getTemp()
Get a Temp Data item.
has()
Tells if the session has an item.
id()
Get/Set the session id.
isActive()
Tells if sessions are enabled, and one exists.
regenerateId()
Update the current session id with a newly generated one.
remove()
Remove (unset) a session item.
removeAll()
Remove (unset) all session items.
removeFlash()
Remove a Flash Data item.
removeMulti()
Remove (unset) multiple session items.
removeTemp()
Remove (unset) a Temp Data item.
reset()
Re-initialize session array with original values.
set()
Set a session item.
setDebugCollector()
-
setFlash()
Set a Flash Data item, available only in the next time the session is started.
setMulti()
Set multiple session items.
setTemp()
Set a Temp Data item.
start()
-
stop()
Write session data and end session.
autoRegenerate()
Auto regenerate the session id.
clearFlash()
Clears the Flash Data.
clearTemp()
Clears the Temp Data.
getOptions()
-
setOptions()
-
setPermanentCookie()
-

__construct()

public function __construct(array $options = array ( ), ?\Framework\Session\SaveHandler $handler = NULL)

Session constructor.

Parameters
  • array $options
  • ?\Framework\Session\SaveHandler $handler

__destruct()

public function __destruct()

__get()

public function __get(string $key): mixed
Parameters
  • string $key
Returns mixed

__isset()

public function __isset(string $key): bool
Parameters
  • string $key
Returns bool

__set()

public function __set(string $key, mixed $value): void
Parameters
  • string $key
  • mixed $value
Returns void

__unset()

public function __unset(string $key): void
Parameters
  • string $key
Returns void

abort()

public function abort(): bool

Discard session data changes and end session.

Returns bool - returns true on success or false on failure

activate()

public function activate(): bool

Make sure the session is active.

If it is not active, it will start it.

Returns bool
throws
RuntimeException if session could not be started

destroy()

public function destroy(): bool

Destroys all data registered to a session.

Returns bool - true on success or false on failure

destroyCookie()

public function destroyCookie(): bool

Sets a Cookie with the session name to be destroyed in the user-agent.

Returns bool - True if the Set-Cookie header was set to invalidate the session cookie, false if output exists
throws
RuntimeException If it could not get the session name

gc()

public function gc(): int|false

Perform session data garbage collection.

If return false, use {@see error_get_last()} to get error details.

Returns int|false - Returns the number of deleted session data for success, false for failure

get()

public function get(string $key): mixed

Gets one session item.

Parameters
  • string $key - The item key name
Returns mixed - The item value or null if no set

getAll()

public function getAll(): array

Get all session items.

Returns array - The value of the $_SESSION global

getFlash()

public function getFlash(string $key): mixed

Get a Flash Data item.

Parameters
  • string $key - The Flash item key name
Returns mixed - The item value or null if not exists

getMulti()

public function getMulti(array $keys): array

Get multiple session items.

Parameters
  • array $keys - An array of key item names
Returns array - An associative array with items keys and values. Item not set will return as null.

getTemp()

public function getTemp(string $key): mixed

Get a Temp Data item.

Parameters
  • string $key - The item key name
Returns mixed - The item value or null if it is expired or not set

has()

public function has(string $key): bool

Tells if the session has an item.

Parameters
  • string $key - The item key name
Returns bool - True if it has, otherwise false

id()

public function id(?string $newId = NULL): string|false

Get/Set the session id.

Parameters
  • ?string $newId - [optional] The new session id
Returns string|false - The old session id or false on failure. Note: If a $newId is set, it is accepted but not validated. When session_start is called, the id is only used if it is valid
throws
LogicException when trying to set a new id and the session is active

isActive()

public function isActive(): bool

Tells if sessions are enabled, and one exists.

Returns bool

regenerateId()

public function regenerateId(bool $deleteOldSession = false): bool

Update the current session id with a newly generated one.

Parameters
  • bool $deleteOldSession - Whether to delete the old associated session item or not
Returns bool

remove()

public function remove(string $key): static

Remove (unset) a session item.

Parameters
  • string $key - The item key name
Returns static

removeAll()

public function removeAll(): static

Remove (unset) all session items.

Returns static

removeFlash()

public function removeFlash(string $key): static

Remove a Flash Data item.

Parameters
  • string $key - The item key name
Returns static

removeMulti()

public function removeMulti(array $keys): static

Remove (unset) multiple session items.

Parameters
  • array $keys - A list of items keys names
Returns static

removeTemp()

public function removeTemp(string $key): static

Remove (unset) a Temp Data item.

Parameters
  • string $key - The item key name
Returns static

reset()

public function reset(): bool

Re-initialize session array with original values.

Returns bool - true if the session was successfully reinitialized or false on failure

set()

public function set(string $key, mixed $value): static

Set a session item.

Parameters
  • string $key - The item key name
  • mixed $value - The item value
Returns static

setDebugCollector()

public function setDebugCollector(\Framework\Session\Debug\SessionCollector $collector): static
Parameters
  • \Framework\Session\Debug\SessionCollector $collector
Returns static

setFlash()

public function setFlash(string $key, mixed $value): static

Set a Flash Data item, available only in the next time the session is started.

Parameters
  • string $key - The Flash Data item key name
  • mixed $value - The item value
Returns static

setMulti()

public function setMulti(array $items): static

Set multiple session items.

Parameters
  • array $items - An associative array of items keys and values
Returns static

setTemp()

public function setTemp(string $key, mixed $value, int $ttl = 60): static

Set a Temp Data item.

Parameters
  • string $key - The item key name
  • mixed $value - The item value
  • int $ttl - The Time-To-Live of the item, in seconds
Returns static

start()

public function start(array $customOptions = array ( )): bool
Parameters
  • array $customOptions
Returns bool
throws
LogicException if session was already active
throws
RuntimeException if session could not be started

stop()

public function stop(): bool

Write session data and end session.

Returns bool - returns true on success or false on failure

autoRegenerate()

protected function autoRegenerate(int $time): void

Auto regenerate the session id.

Parameters
  • int $time
Returns void

clearFlash()

protected function clearFlash(): void

Clears the Flash Data.

Returns void

clearTemp()

protected function clearTemp(int $time): void

Clears the Temp Data.

Parameters
  • int $time - The max time to temp data survive
Returns void

getOptions()

protected function getOptions(array $custom = array ( )): array
Parameters
  • array $custom
Returns array

setOptions()

protected function setOptions(array $custom): void
Parameters
  • array $custom
Returns void

setPermanentCookie()

protected function setPermanentCookie(int $time): void
Parameters
  • int $time
Returns void