HelPHP

Sessions
in package

Tags
class

Sessions

Handles session management for the application, supporting both Redis and database-backed sessions. Provides methods for session initialization, ID regeneration, reading, writing, destroying, and garbage collection. Also manages session references and supports secure cookie settings.

Table of Contents

Properties

$debug  : bool
$instance  : object
$purge_delay  : int
$redis  : bool

Methods

__construct()  : mixed
check_key()  : string|false
Checks and updates the session key and internal ID.
close()  : mixed
create_id()  : string
Generates a new session ID.
create_instance()  : global
Creates or returns the instance of the Sessions class as global $SESSION this global is used essentialy in this class, because using the classic PHP $_SESSION global will be the same.
destroy()  : bool
Destroys a session and removes its references from the database.
gc()  : int
Garbage collector for sessions. Removes expired sessions from the database.
init_db()  : void
Checks and creates the necessary database tables for session storage if Redis is not used and those tables missing.
open()  : bool
Opens the session handler (required by session_set_save_handler).
open_session()  : void
Opens and configures the PHP session, setting cookie parameters and session lifetime.
read()  : string
Reads session data from the database.
update_id()  : void|false
Regenerates the session ID at a given interval when using database sessions.
update_id_redis()  : void
Regenerates the session ID at a given interval when using Redis.
write()  : bool
Writes session data to the database.

Properties

$debug

public static bool $debug = false

Enable debug logging for session operations.

$instance

public static object $instance = null

instance of the Sessions class.

$purge_delay

public int $purge_delay = 0

(in seconds) before purging old session references.

$redis

public bool $redis = true

is used for session storage.

Methods

__construct()

public __construct() : mixed

check_key()

Checks and updates the session key and internal ID.

public check_key(string $key) : string|false
Parameters
$key : string

The session key to check.

Return values
string|false

The valid session key, or false if not found.

create_id()

Generates a new session ID.

public create_id() : string
Return values
string

The new session ID.

create_instance()

Creates or returns the instance of the Sessions class as global $SESSION this global is used essentialy in this class, because using the classic PHP $_SESSION global will be the same.

public static create_instance([bool $forceNewInstance = false ]) : global

Initializes session handling, sets up Redis or database as backend, and configures session cookie parameters.

Parameters
$forceNewInstance : bool = false

If true, forces creation of a new instance.

Return values
global

$SESSION The session instance.

destroy()

Destroys a session and removes its references from the database.

public destroy(string $key) : bool
Parameters
$key : string

The session key.

Return values
bool

True on success.

gc()

Garbage collector for sessions. Removes expired sessions from the database.

public gc([int $maxlifetime = 0 ]) : int
Parameters
$maxlifetime : int = 0

Maximum session lifetime in seconds.

Return values
int

Number of sessions deleted.

init_db()

Checks and creates the necessary database tables for session storage if Redis is not used and those tables missing.

public init_db() : void

open()

Opens the session handler (required by session_set_save_handler).

public open(string $save_path, string $sessionName) : bool
Parameters
$save_path : string

Path where to store/retrieve session.

$sessionName : string

Name of the session.

Return values
bool

Always returns true.

open_session()

Opens and configures the PHP session, setting cookie parameters and session lifetime.

public static open_session([int $update_interval = 300 ][, int $session_life_time = 24 ]) : void
Parameters
$update_interval : int = 300

Interval (in seconds) for session ID regeneration. Default 300.

$session_life_time : int = 24

Session lifetime in hours. Default 24.

read()

Reads session data from the database.

public read(string $key) : string
Parameters
$key : string

The session key.

Return values
string

The session data, or an empty string if not found.

update_id()

Regenerates the session ID at a given interval when using database sessions.

public static update_id([int $interval = 300 ]) : void|false

Updates session references and purges old session IDs.

Parameters
$interval : int = 300

Interval (in seconds) for session ID regeneration. Minimum 10.

Return values
void|false

Returns false if update is not needed.

update_id_redis()

Regenerates the session ID at a given interval when using Redis.

public static update_id_redis([int $interval = 300 ]) : void
Parameters
$interval : int = 300

Interval (in seconds) for session ID regeneration. Minimum 300.

write()

Writes session data to the database.

public write(string $key, string $val) : bool
Parameters
$key : string

The session key.

$val : string

The session data to write.

Return values
bool

True on success.


        
On this page

Search results