Luminova Framework

PHP Luminova: Backend Client Session Management

Last updated: 2024-08-28 22:29:34

The Session class provides a set of methods to simplify session management in your Luminova application. It offers convenient methods for storing, retrieving, and manipulating session data.

The Luminova's Session Manager is a useful component that provides a set of methods to simplify managing user sessions in your web applications. Its primary function is to enhance the storage and retrieval of session data, allowing applications to maintain stateful interactions with users across multiple requests. With support for two distinct storage drivers SessionManager and CookieManager, you have the flexibility to choose the appropriate session storage mechanism based on your need.

This is primarily designed for server-side session storage for enhanced security and scalability.


  • Class namespace: \Luminova\Sessions\Session

Methods

constructor

Initializes session class with your preferred manager class, such as Luminova\Sessions\SessionManager or Luminova\Sessions\CookieManager.

If the argument is passed NULL the SessionManager will be used as default.

new Session(\Luminova\Interface\SessionManagerInterface|null $manager = null): mixed

Parameters:

ParameterTypeDescription
$manager\Luminova\Interface\SessionManagerInterface|nullThe session manager.

getInstance

Get a shared instance of the Session class.

public static getInstance(?\Luminova\Interface\SessionManagerInterface $manager = null): static

Parameters:

ParameterTypeDescription
$manager\Luminova\Interface\SessionManagerInterface|nullThe session manager class.

Return Value:

static - Session class instance.


toArray

Retrieve data as an array from the current session storage.

public toArray(string $index = ''): array

Parameters:

ParameterTypeDescription
$indexstringOptional key to retrieve.

Return Value:

array - Return an array representation of data


toObject

Retrieves data as an object from the current session storage.

public toObject(string $index = ''): object

Parameters:

ParameterTypeDescription
$indexstringOptional key to retrieve.

Return Value:

object - Return an object representation of data.


toExport

Retrieves all stored session data as an array or object.

public toExport(string $type = 'array'): array|object

Parameters:

ParameterTypeDescription
$typestringReturn type of object or array (default is 'array').

Return Value:

array|object - All stored session data.


setManager

Sets the session manager.

public setManager(\Luminova\Interface\SessionManagerInterface $manager): void

Parameters:

ParameterTypeDescription
$manager\Luminova\Interface\SessionManagerInterfaceThe session manager to set.

getManager

Retrieves the session storage manager instance (CookieManager or SessionManager).

public getManager(): \Luminova\Interface\SessionManagerInterface|null

Return Value:

SessionManagerInterface - The storage manager instance otherwise null if not set.


setStorage

Sets the storage name to store and retrieve items from.Each time you call method for CRUD operations it will use your storage, if you want to to change storage you can set new storage name before calling methods.

public setStorage(string $storage): self

Parameters:

ParameterTypeDescription
$storagestringThe storage key to set.

Return Value:

Session - Return session class instance.


getStorage

Retrieves the current session storage name.

public getStorage(): string

Return Value:

string - Return the current storage name.


get

Retrieves a value from the session storage.

public get(string $key, mixed $default = null): mixed

Parameters:

ParameterTypeDescription
$keystringThe key to retrieve.
$defaultmixedDefault value if the key is not found.

Return Value:

mixed - Return the retrieved data.


getFrom

Retrieves an item from a specified session storage instance.

public getFrom(string $index, string $storage): mixed

Parameters:

ParameterTypeDescription
$indexstringThe key to retrieve.
$storagestringThe storage key name.

Return Value:

mixed - Return the retrieved data.


setTo

Sets an item to a specified session storage instance.

public setTo(string $index, mixed $data, string $storage): self

Parameters:

ParameterTypeDescription
$indexstringThe key to set.
$datamixedThe data to set.
$storagestringThe storage key name.

Return Value:

self - Return session class instance.


online

Checks if user has successfully logged in online.

public online(string $storage = ''): bool

Parameters:

ParameterTypeDescription
$storagestringoptional storage instance key

Return Value:

bool - Returns true if the session user is online, false otherwise.

Optionally, specify a storage name to check; otherwise, it checks the current storage.


ssid

Retrieves the user's login session ID.

public ssid(): string|null

Return Value:

string|null - Returns the session ID or null if not logged in.

A unique session ID is automatically generated once synchronize() is called.


ssdate

Retrieves the user's login session datetime in ISO 8601 format.

public ssdate(): string|null

Return Value:

string|null - Returns the session login date-time or null if not logged in.

The session datetime is automatically generated once synchronize() is called.


set

Store data in the session storage.

public set(string $key, mixed $value): self

Parameters:

ParameterTypeDescription
$keystringThe key to set.
$valuemixedThe value to set.

Return Value:

Session - Return session class instance.


add

Adds an item to the session storage without overwriting existing values.

public add(string $key, mixed $value): bool

Parameters:

ParameterTypeDescription
$keystringThe key to set.
$valuemixedThe value to set.

Return Value:

bool - Return true if item was added else false.


remove

Remove a key from the session storage by passing the key.

public remove(string $key): self

Parameters:

ParameterTypeDescription
$keystringThe key to remove.

Return Value:

Session - Return session class instance.


clear

Clear all data from session storage by passing the storage key.

public clear(string $storage = ''): self

Parameters:

ParameterTypeDescription
$storagestringOptionally pass storage name to clear.

Return Value:

Session - Return session class instance.


has

Check if item key exists in session storage.

public has(string $key): bool

Parameters:

ParameterTypeDescription
$keystringKey to check

Return Value:

boo - Return true if key exists in session storage else false.


start

To start a session, simply call the start() method. It initializes the session if it's not already started.

public start(?string $sid = null): void

Parameters:

ParameterTypeDescription
$sidstring|nullOptional specify session identifier from PHP function session_id.

This method replaces the default PHP session_start(), but with additional configuration.


destroy

To empty all data stored in your application session table index.

public destroy(): bool

Return Value

bool - Return true if storage was data was deleted successfully otherwise false.

This method doesn't behave same way as PHP session_destroy.


synchronize

Starts a user's online login session, optionally specifying an IP address.

public synchronize(string $ip = ''): self

Parameters:

ParameterTypeDescription
$ipstringThe IP address.

Return Value:

Session - Return session class instance.

This method should be called to indicate that the user has successfully logged in.


ipChanged

Checks if the user's IP address has changed since the last login session.

public ipChanged(string $storage = ''): bool

Parameters:

ParameterTypeDescription
$storagestringOptional storage location

Return Value:

bool - Returns false if the user's IP address matches the session login IP, otherwise returns true.


Examples

Initializes the session storage driver of your choice.

$session = new Session(new SessionManager());
$session->start();

Cookie manager, this manager will store your user information in cookie storage which is client-side storage.

$session = new Session(new CookieManager());
$session->start();

By default access to JavaScript is not allowed on and cookie should only be transmitted over secure HTTPS connection.