SessionManager
in package
uses
Loggable
SessionManager handles PHP session management with enhanced security and configuration options
Provides methods for session handling including:
- Secure session configuration
- Session timeout management
- Namespaced session data
- CSRF token generation
- Read-only session access
Table of Contents
Properties
- $httpOnly : bool
- $lifetime : int
- $savePath : string
- $secure : bool
- $sessionName : string
- $started : bool
- $logger : object|null
Methods
- __construct() : mixed
- SessionManager constructor
- checkSessionStatus() : bool
- Check if session is active
- checkTimeout() : void
- Check if session has timed out based on last activity
- close() : void
- Close session early to prevent writing conflicts
- destroy() : void
- Destroy current session
- enforceCookieOnlySession() : void
- Enforce cookie-only session and remove PHPSESSID from URL if present
- get() : mixed
- Get session value
- getCsrfToken() : string
- Get or generate CSRF token
- getNamespace() : mixed
- Get namespaced session value
- has() : bool
- Check if session key exists
- id() : string
- Get current session ID
- log() : void
- name() : string
- Get session name
- readOnlyStart() : void
- Start session in read-only mode
- regenerate() : void
- Regenerate session ID
- remove() : void
- Remove session value
- set() : void
- Set session value
- setNamespace() : void
- Set namespaced session value
- start() : void
- Start session if not already started
- configure() : void
- Configure session settings
Properties
$httpOnly
protected
bool
$httpOnly
$lifetime
protected
int
$lifetime
$savePath
protected
string
$savePath
$secure
protected
bool
$secure
$sessionName
protected
string
$sessionName
$started
protected
bool
$started
= false
$logger
private
object|null
$logger
= null
Methods
__construct()
SessionManager constructor
public
__construct([string $sessionName = 'UNNAMED_SESSION' ][, string $savePath = 'temp/sessions' ][, int $lifetime = 3600 ][, bool $secure = false ][, bool $httpOnly = true ]) : mixed
Parameters
- $sessionName : string = 'UNNAMED_SESSION'
-
Name of the session (default: 'UNNAMED_SESSION')
- $savePath : string = 'temp/sessions'
-
Path to store session files (default: 'temp/sessions')
- $lifetime : int = 3600
-
Session lifetime in seconds (default: 3600)
- $secure : bool = false
-
Whether to use secure cookies (default: false)
- $httpOnly : bool = true
-
Whether to make cookies HTTP only (default: true)
Tags
checkSessionStatus()
Check if session is active
public
checkSessionStatus() : bool
Return values
bool —True if session is active, false otherwise
checkTimeout()
Check if session has timed out based on last activity
public
checkTimeout() : void
close()
Close session early to prevent writing conflicts
public
close() : void
destroy()
Destroy current session
public
destroy() : void
enforceCookieOnlySession()
Enforce cookie-only session and remove PHPSESSID from URL if present
public
enforceCookieOnlySession([bool $redirectIfUrlHasSid = true ]) : void
Parameters
- $redirectIfUrlHasSid : bool = true
-
Whether to redirect if session ID is in URL (default: true)
get()
Get session value
public
get(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
Session key
- $default : mixed = null
-
Default value if key not found
Return values
mixed —Stored value or default
getCsrfToken()
Get or generate CSRF token
public
getCsrfToken() : string
Return values
string —CSRF token
getNamespace()
Get namespaced session value
public
getNamespace(string $namespace, string $key[, mixed $default = null ]) : mixed
Parameters
- $namespace : string
-
Namespace
- $key : string
-
Session key
- $default : mixed = null
-
Default value if key not found
Return values
mixed —Stored value or default
has()
Check if session key exists
public
has(string $key) : bool
Parameters
- $key : string
-
Session key
Return values
bool —True if key exists, false otherwise
id()
Get current session ID
public
id() : string
Return values
string —Session ID
log()
public
log([mixed $message = '' ][, string $level = 'info' ][, mixed $backtrace = null ][, int $spacing = 0 ]) : void
Parameters
- $message : mixed = ''
- $level : string = 'info'
- $backtrace : mixed = null
- $spacing : int = 0
name()
Get session name
public
name() : string
Return values
string —Session name
readOnlyStart()
Start session in read-only mode
public
readOnlyStart() : void
regenerate()
Regenerate session ID
public
regenerate([bool $deleteOldSession = true ]) : void
Parameters
- $deleteOldSession : bool = true
-
Whether to delete old session (default: true)
Tags
remove()
Remove session value
public
remove(string $key) : void
Parameters
- $key : string
-
Session key to remove
set()
Set session value
public
set(string $key, mixed $value) : void
Parameters
- $key : string
-
Session key
- $value : mixed
-
Value to store
setNamespace()
Set namespaced session value
public
setNamespace(string $namespace, string $key, mixed $value) : void
Parameters
- $namespace : string
-
Namespace
- $key : string
-
Session key
- $value : mixed
-
Value to store
start()
Start session if not already started
public
start() : void
configure()
Configure session settings
protected
configure() : void