Documentation

SessionManager
in package

SessionManager - PHP Session Management Wrapper

Encapsulates $_SESSION access to prevent direct manipulation and facilitate unit testing with dependency injection.

Features:

  • Safe session start/stop
  • Flash messages (one-time messages)
  • Session regeneration for security
  • Consistent API for session operations
Tags
version
1.0.0

Table of Contents

Properties

$started  : bool

Methods

clear()  : void
Clear all session data
destroy()  : void
Destroy the session completely
flash()  : void
Set a flash message (available only once)
get()  : mixed
Get a session value
getFlash()  : mixed
Get and consume a flash message
getId()  : string
Get the current session ID
has()  : bool
Check if a session key exists
hasFlash()  : bool
Check if a flash message exists
isStarted()  : bool
Check if the session is started
regenerate()  : void
Regenerate the session ID (security measure)
remove()  : void
Remove a session value
set()  : void
Set a session value
start()  : void
Start the PHP session
ensureStarted()  : void
Ensure the session is started

Properties

Methods

clear()

Clear all session data

public clear() : void

Removes all session variables but keeps the session active.

destroy()

Destroy the session completely

public destroy() : void

Clears all data, deletes session cookie, and destroys the session. Use this for logout operations.

flash()

Set a flash message (available only once)

public flash(string $key, mixed $value) : void

Flash messages are automatically deleted after being read once. Useful for success/error messages after redirects.

Parameters
$key : string

Message type (success, error, warning, info)

$value : mixed

Message content

get()

Get a session value

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

Session key

$default : mixed = null

Default value if key doesn't exist

Return values
mixed

The session value or default

getFlash()

Get and consume a flash message

public getFlash(string $key[, mixed $default = null ]) : mixed

Retrieves the flash message and immediately deletes it. Subsequent calls return null.

Parameters
$key : string

Message type

$default : mixed = null

Default value if flash doesn't exist

Return values
mixed

The flash message or default

getId()

Get the current session ID

public getId() : string
Return values
string

The session identifier

has()

Check if a session key exists

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

Session key to check

Return values
bool

True if key exists

hasFlash()

Check if a flash message exists

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

Message type to check

Return values
bool

True if flash message exists

isStarted()

Check if the session is started

public isStarted() : bool
Return values
bool

True if session is active

regenerate()

Regenerate the session ID (security measure)

public regenerate([bool $deleteOldSession = true ]) : void

Should be called after login to prevent session fixation attacks.

Parameters
$deleteOldSession : bool = true

Whether to delete the old session file

remove()

Remove a session value

public remove(string $key) : void
Parameters
$key : string

Session key to remove

set()

Set a session value

public set(string $key, mixed $value) : void
Parameters
$key : string

Session key

$value : mixed

Value to store

start()

Start the PHP session

public start() : void

Safe to call multiple times - checks if session is already active.

ensureStarted()

Ensure the session is started

private ensureStarted() : void

Internal helper that automatically starts the session if needed.


        
On this page

Search results