Session
in package
Session class
This class is responsible for managing the session.
It provides methods to set the title, add CSS and JavaScript files, add meta tags, add link tags, add property tags, add generator tags and set other head section attributes.
Tags
Table of Contents
Constants
- FLASH_KEY : mixed = 'flash_messages'
Properties
- $config : array<string|int, mixed>
- $flashMarked : mixed
Methods
- __construct() : mixed
- Constructs a new Session object.
- __destruct() : void
- Destructor of the Session class.
- get() : mixed
- Gets a value from the session.
- getFlash() : mixed
- Gets a flash message from the session.
- newSession() : void
- Generates a new session id.
- remove() : void
- Removes a value from the session.
- set() : void
- Sets a value in the session.
- setFlash() : void
- Sets a flash message in the session.
- sessionDestroy() : void
- Destroys the session and removes all session data.
- ensureFlashMarked() : void
- Marks all the flash messages for removal.
- ensureSessionStarted() : void
- Ensures that the session has been started.
- removeFlashMessages() : void
- Removes all flash messages from the session.
Constants
FLASH_KEY
protected
mixed
FLASH_KEY
= 'flash_messages'
Properties
$config
private
array<string|int, mixed>
$config
$flashMarked
private
mixed
$flashMarked
= false
Methods
__construct()
Constructs a new Session object.
public
__construct(array<string|int, mixed> $config) : mixed
This object is responsible for managing the session.
It provides methods to set the title, add CSS and JavaScript files, add meta tags, add link tags, add property tags, add generator tags and set other head section attributes.
Parameters
- $config : array<string|int, mixed>
-
The configuration for the session. The configuration should contain the following keys:
- sessionName: The name of the session.
- lifetime: The lifetime of the session in seconds.
- expireOnClose: Whether the session should expire when the browser is closed.
- path: The path where the session cookie should be available.
- domain: The domain where the session cookie should be available.
- secure: Whether the session cookie should be secure.
- httpOnly: Whether the session cookie should be available over HTTP.
- sameSite: Whether the session cookie should be available on the same site.
__destruct()
Destructor of the Session class.
public
__destruct() : void
This method is called when the object is destroyed. It will remove all flash messages from the session.
get()
Gets a value from the session.
public
get(string $key) : mixed
This method will get a value from the session. The value is set with the set() method. The value will be removed when the session is closed.
Parameters
- $key : string
-
The key of the value.
Return values
mixed —The value of the key or false if the key does not exist.
getFlash()
Gets a flash message from the session.
public
getFlash(string $key) : mixed
This method will get a flash message from the session. The flash message is set with the setFlash() method. The flash message will be removed when the session is closed.
Parameters
- $key : string
-
The key of the flash message.
Return values
mixed —The value of the flash message or false if the flash message does not exist.
newSession()
Generates a new session id.
public
newSession() : void
This method will generate a new session id and regenerate the session. This is useful when a user logs in or out.
remove()
Removes a value from the session.
public
remove(string $key) : void
This method will remove a value from the session. The value is set with the set() method. The value will be removed when the session is closed.
Parameters
- $key : string
-
The key of the value.
set()
Sets a value in the session.
public
set(string $key, mixed $value) : void
This method will set a value in the session. The value can be retrieved with the get() method. The value will be removed when the session is closed.
Parameters
- $key : string
-
The key of the value.
- $value : mixed
-
The value to set.
setFlash()
Sets a flash message in the session.
public
setFlash(string $key, mixed $message) : void
This method will set a flash message in the session. The flash message can be retrieved with the getFlash() method. The flash message will be removed when the session is closed.
Parameters
- $key : string
-
The key of the flash message.
- $message : mixed
-
The value of the flash message.
sessionDestroy()
Destroys the session and removes all session data.
protected
sessionDestroy() : void
This method is called when the object is destroyed. It will remove all session data from the session.
ensureFlashMarked()
Marks all the flash messages for removal.
private
ensureFlashMarked() : void
This method will mark all the flash messages for removal. The flash messages will be removed when the session is closed.
ensureSessionStarted()
Ensures that the session has been started.
private
ensureSessionStarted([bool $force = false ]) : void
If the session has not been started yet, this method will start it. If the session has already been started, this method will do nothing.
Parameters
- $force : bool = false
-
Whether to force the session to be started even if it has already been started.
removeFlashMessages()
Removes all flash messages from the session.
private
removeFlashMessages() : void
This method is called when the session is closed. It will remove all flash messages from the session.